Skip to content

Commit 086ae1a

Browse files
committed
style: formating
1 parent ede8b39 commit 086ae1a

File tree

2 files changed

+102
-96
lines changed

2 files changed

+102
-96
lines changed

main.py

Lines changed: 101 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from hashlib import sha3_512
21
import hashlib
32
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
43
from cryptography.hazmat.primitives import hashes
@@ -14,126 +13,133 @@
1413
import os
1514
import subprocess
1615

16+
1717
def get_serial_number():
18-
try:
19-
system = platform.system()
20-
if system == 'Windows':
21-
return platform.win32_machineserial()
22-
elif system == 'Linux':
23-
if os.geteuid() != 0:
24-
print("Process needs to be root.")
25-
subprocess.call(['sudo','-E', 'python3', *sys.argv])
26-
sys.exit()
27-
with open('/sys/class/dmi/id/product_serial') as f:
28-
return f.read().strip()
29-
elif system == 'Darwin':
30-
return platform.system_profiler().get('Hardware').get('Serial Number')
31-
else:
32-
return None
33-
except Exception:
34-
return None
35-
18+
try:
19+
system = platform.system()
20+
if system == 'Windows':
21+
return platform.win32_machineserial()
22+
elif system == 'Linux':
23+
if os.geteuid() != 0:
24+
print("Process needs to be root.")
25+
subprocess.call(['sudo', '-E', 'python3', *sys.argv])
26+
sys.exit()
27+
with open('/sys/class/dmi/id/product_serial') as file:
28+
return file.read().strip()
29+
elif system == 'Darwin':
30+
return platform.system_profiler().get('Hardware').get('Serial Number')
31+
else:
32+
return None
33+
except Exception as error:
34+
print(f"Unexpected error: {error}")
35+
sys.exit(1)
36+
37+
3638
serial_number = get_serial_number()
3739
if not serial_number:
38-
print("Impossible de récupérer le numéro de série.")
39-
sys.exit(1)
40+
print("Impossible de récupérer le numéro de série.")
41+
sys.exit(1)
4042
else:
41-
print("Serial number : ", serial_number)
43+
print("Serial number : ", serial_number)
4244

4345
try:
44-
serial_number = str(serial_number)
45-
hash_serial = hashlib.sha3_512(serial_number.encode())
46+
serial_number = str(serial_number)
47+
hash_serial = hashlib.sha3_512(serial_number.encode())
4648

47-
parser = argparse.ArgumentParser()
49+
parser = argparse.ArgumentParser()
4850

49-
parser.add_argument('-p', '--path', dest='path', default="./machine.lic", help='Path to machine file')
50-
parser.add_argument('-l', '--license', dest='license', default='key/TEg3TS05VldLLUpKSFUtN0NSVC1NUEtSLUg5VUwtOU1GNy03VjlK.hphP_9YaFq0uZykkfH0l9xEmogJ4yUbo3Wym7oIxYgl0uNBwocsS3GZse6U2Ti2a8B09iB5-gi_ilr3V05z4Dw==', help='License key')
51-
parser.add_argument('-f', '--fingerprint', dest='fingerprint', default=hash_serial.hexdigest(), help='Machine fingerprint')
51+
parser.add_argument('-p', '--path', dest='path', default="./machine.lic", help='Path to machine file')
52+
parser.add_argument('-l', '--license', dest='license',
53+
default='key/TEg3TS05VldLLUpKSFUtN0NSVC1NUEtSLUg5VUwtOU1GNy03VjlK'
54+
'.hphP_9YaFq0uZykkfH0l9xEmogJ4yUbo3Wym7oIxYgl0uNBwocsS3GZse6U2Ti2a8B09iB5'
55+
'-gi_ilr3V05z4Dw==',
56+
help='License key')
57+
parser.add_argument('-f', '--fingerprint', dest='fingerprint', default=hash_serial.hexdigest(),
58+
help='Machine fingerprint')
5259

53-
KEYGEN_PUBLIC_KEY = '7757a98a8188c31ae7a21d76a865800bf77bcf3476f7abbbdf5bb6a4afbe9a23'
60+
KEYGEN_PUBLIC_KEY = '7757a98a8188c31ae7a21d76a865800bf77bcf3476f7abbbdf5bb6a4afbe9a23'
5461

55-
args = parser.parse_args()
62+
args = parser.parse_args()
5663

57-
# Read the machine file
58-
machine_file = None
64+
# Read the machine file
65+
machine_file = None
5966

60-
try:
61-
with open(args.path) as f:
62-
machine_file = f.read()
63-
except (FileNotFoundError, PermissionError):
64-
print('[error] path does not exist! (or permission was denied)')
67+
try:
68+
with open(args.path) as f:
69+
machine_file = f.read()
70+
except (FileNotFoundError, PermissionError):
71+
print('[error] path does not exist! (or permission was denied)')
6572

66-
sys.exit(1)
73+
sys.exit(1)
6774

68-
# Strip the header and footer from the machine file certificate
69-
payload = machine_file.lstrip('-----BEGIN MACHINE FILE-----\n') \
70-
.rstrip('-----END MACHINE FILE-----\n')
75+
# Strip the header and footer from the machine file certificate
76+
payload = machine_file.lstrip('-----BEGIN MACHINE FILE-----\n') \
77+
.rstrip('-----END MACHINE FILE-----\n')
7178

72-
# Decode the payload and parse the JSON object
73-
data = json.loads(base64.b64decode(payload))
79+
# Decode the payload and parse the JSON object
80+
data = json.loads(base64.b64decode(payload))
7481

75-
# Retrieve the enc and sig properties
76-
enc = data['enc']
77-
sig = data['sig']
78-
alg = data['alg']
82+
# Retrieve the enc and sig properties
83+
enc = data['enc']
84+
sig = data['sig']
85+
alg = data['alg']
7986

80-
if alg != 'aes-256-gcm+ed25519':
81-
print('[error] algorithm is not supported!')
87+
if alg != 'aes-256-gcm+ed25519':
88+
print('[error] algorithm is not supported!')
8289

83-
sys.exit(1)
90+
sys.exit(1)
8491

85-
# Verify using Ed25519
86-
try:
87-
verify_key = ed25519.VerifyingKey(
88-
KEYGEN_PUBLIC_KEY.encode(),
89-
encoding='hex',
90-
)
92+
# Verify using Ed25519
93+
try:
94+
verify_key = ed25519.VerifyingKey(
95+
KEYGEN_PUBLIC_KEY.encode(),
96+
encoding='hex',
97+
)
9198

92-
verify_key.verify(
93-
base64.b64decode(sig),
94-
('machine/%s' % enc).encode(),
95-
)
96-
except (AssertionError, BadSignatureError):
97-
print('[error] verification failed!')
99+
verify_key.verify(
100+
base64.b64decode(sig),
101+
('machine/%s' % enc).encode(),
102+
)
103+
except (AssertionError, BadSignatureError):
104+
print('[error] verification failed!')
98105

99-
sys.exit(1)
106+
sys.exit(1)
107+
108+
print('[info] verification successful!')
109+
110+
# Hash the license key and fingerprint using SHA256
111+
digest = hashes.Hash(hashes.SHA256())
112+
digest.update(args.license.encode())
113+
digest.update(args.fingerprint.encode())
114+
key = digest.finalize()
100115

101-
print('[info] verification successful!')
102-
103-
# Hash the license key and fingerprint using SHA256
104-
digest = hashes.Hash(hashes.SHA256())
105-
digest.update(args.license.encode())
106-
digest.update(args.fingerprint.encode())
107-
key = digest.finalize()
108-
109-
# Split and decode the enc value
110-
ciphertext, iv, tag = map(
111-
lambda p: base64.b64decode(p),
112-
enc.split('.'),
113-
)
114-
115-
# Decrypt ciphertext
116-
try:
117-
aes = Cipher(
118-
algorithms.AES(key),
119-
modes.GCM(iv, None, len(tag)),
120-
default_backend(),
116+
# Split and decode the enc value
117+
ciphertext, iv, tag = map(
118+
lambda p: base64.b64decode(p),
119+
enc.split('.'),
121120
)
122-
dec = aes.decryptor()
123121

124-
plaintext = dec.update(ciphertext) + \
125-
dec.finalize_with_tag(tag)
126-
except (InvalidKey, InvalidTag):
127-
print('[error] decryption failed!')
122+
# Decrypt ciphertext
123+
try:
124+
aes = Cipher(
125+
algorithms.AES(key),
126+
modes.GCM(iv, None, len(tag)),
127+
default_backend(),
128+
)
129+
dec = aes.decryptor()
128130

129-
sys.exit(1)
131+
plaintext = dec.update(ciphertext) + dec.finalize_with_tag(tag)
132+
except (InvalidKey, InvalidTag):
133+
print('[error] decryption failed!')
134+
135+
sys.exit(1)
130136

131-
print('[info] decryption successful!')
132-
"""print(
137+
print('[info] decryption successful!')
138+
"""print(
133139
json.dumps(json.loads(plaintext.decode()), indent=2)
134140
)"""
135-
except(Exception):
136-
print("License verification failed, check your license.")
137-
sys.exit(1)
141+
except Exception as error:
142+
print("License verification failed, check your license: " + str(error))
143+
sys.exit(1)
138144

139-
print("Hello, World!")
145+
print("Hello, World!")

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
py-machineid
1+
py-machineid==0.4.1
22
argparse
33
cryptography
44
ed25519

0 commit comments

Comments
 (0)