Skip to content

Commit 54beb6e

Browse files
committed
feat: better implementation of main
1 parent b62e034 commit 54beb6e

File tree

1 file changed

+95
-88
lines changed

1 file changed

+95
-88
lines changed

main.py

Lines changed: 95 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -35,120 +35,127 @@ def get_serial_number():
3535
return
3636

3737

38-
try:
39-
# Definition of all constants and variables needed
40-
parser = argparse.ArgumentParser()
41-
parser.add_argument('-p', '--path-machine', dest='path_machine', default="./machine.lic", help='Path to machine '
42-
'file')
43-
parser.add_argument('-l', '--path-license', dest='path_license', default='./license.lic', help='Path to license '
44-
'key file')
45-
parser.add_argument('-f', '--fingerprint', dest='fingerprint', help='Machine fingerprint')
46-
parser.add_argument('-pk', '--public-key', dest='public_key',
47-
default='e8601e48b69383ba520245fd07971e983d06d22c4257cfd82304601479cee788')
48-
args = parser.parse_args()
49-
machine_file = None
50-
license_file = None
51-
52-
if args.fingerprint is None:
53-
serial_number = get_serial_number()
54-
if not serial_number:
55-
print("Unable to get serial number. Is your system compatible? Compatible systems : Windows, Linux")
56-
sys.exit(1)
57-
else:
58-
print("Serial number : ", serial_number)
59-
60-
serial_number = str(serial_number)
61-
hash_serial = hashlib.sha3_512(serial_number.encode())
62-
args.fingerprint = hash_serial.hexdigest()
38+
def main():
39+
try:
40+
# Definition of all constants and variables needed
41+
parser = argparse.ArgumentParser()
42+
parser.add_argument('-p', '--path-machine', dest='path_machine', default="./machine.lic",
43+
help='Path to machine '
44+
'file')
45+
parser.add_argument('-l', '--path-license', dest='path_license', default='./license.lic',
46+
help='Path to license '
47+
'key file')
48+
parser.add_argument('-f', '--fingerprint', dest='fingerprint', help='Machine fingerprint')
49+
parser.add_argument('-pk', '--public-key', dest='public_key',
50+
default='e8601e48b69383ba520245fd07971e983d06d22c4257cfd82304601479cee788')
51+
args = parser.parse_args()
52+
machine_file = None
53+
license_file = None
54+
55+
if args.fingerprint is None:
56+
serial_number = get_serial_number()
57+
if not serial_number:
58+
print("Unable to get serial number. Is your system compatible? Compatible systems : Windows, Linux")
59+
return
60+
else:
61+
print("Serial number : ", serial_number)
62+
63+
serial_number = str(serial_number)
64+
hash_serial = hashlib.sha3_512(serial_number.encode())
65+
args.fingerprint = hash_serial.hexdigest()
6366

6467
print("Replace \"PUBLIC_KEY\" line 66 with your public key (\"Ed25519 128-bit Verify Key\") available in https://app.keygen.sh/settings. Then comment lines 64 & 65 and run again.")
6568
sys.exit(1) # Comment this line to continue
6669
args.public_key = 'PUBLIC_KEY'
6770

68-
# Read the license key file
69-
try:
70-
with open(args.path_license) as f_license:
71-
license_key = f_license.read()
72-
except (FileNotFoundError, PermissionError):
73-
print('[error] license path does not exist! (or permission was denied)')
71+
# Read the license key file
72+
try:
73+
with open(args.path_license) as f_license:
74+
license_key = f_license.read()
75+
except (FileNotFoundError, PermissionError):
76+
print('[error] license path does not exist! (or permission was denied)')
7477

7578
return
7679

77-
# Read the machine file
78-
try:
79-
with open(args.path_machine) as f_machine:
80-
machine_file = f_machine.read()
81-
except (FileNotFoundError, PermissionError):
82-
print('[error] machine path does not exist! (or permission was denied)')
80+
# Read the machine file
81+
try:
82+
with open(args.path_machine) as f_machine:
83+
machine_file = f_machine.read()
84+
except (FileNotFoundError, PermissionError):
85+
print('[error] machine path does not exist! (or permission was denied)')
8386

8487
return
8588

86-
# Strip the header and footer from the machine file certificate
87-
payload = machine_file.lstrip('-----BEGIN MACHINE FILE-----\n') \
88-
.rstrip('-----END MACHINE FILE-----\n')
89+
# Strip the header and footer from the machine file certificate
90+
payload = machine_file.lstrip('-----BEGIN MACHINE FILE-----\n') \
91+
.rstrip('-----END MACHINE FILE-----\n')
8992

90-
# Decode the payload and parse the JSON object
91-
data = json.loads(base64.b64decode(payload))
93+
# Decode the payload and parse the JSON object
94+
data = json.loads(base64.b64decode(payload))
9295

93-
# Retrieve the enc and sig properties
94-
enc = data['enc']
95-
sig = data['sig']
96-
alg = data['alg']
96+
# Retrieve the enc and sig properties
97+
enc = data['enc']
98+
sig = data['sig']
99+
alg = data['alg']
97100

98-
if alg != 'aes-256-gcm+ed25519':
99-
print('[error] algorithm is not supported!')
101+
if alg != 'aes-256-gcm+ed25519':
102+
print('[error] algorithm is not supported!')
100103

101104
return
102105

103-
# Verify using Ed25519
104-
try:
105-
verify_key = ed25519.VerifyingKey(
106-
args.public_key.encode(),
107-
encoding='hex',
108-
)
106+
# Verify using Ed25519
107+
try:
108+
verify_key = ed25519.VerifyingKey(
109+
args.public_key.encode(),
110+
encoding='hex',
111+
)
109112

110-
verify_key.verify(
111-
base64.b64decode(sig),
112-
('machine/%s' % enc).encode(),
113-
)
114-
except (AssertionError, BadSignatureError):
115-
print('[error] verification failed!')
113+
verify_key.verify(
114+
base64.b64decode(sig),
115+
('machine/%s' % enc).encode(),
116+
)
117+
except (AssertionError, BadSignatureError):
118+
print('[error] verification failed!')
116119

117120
return
118121

119-
print('[info] machine file verification successful!')
122+
print('[info] machine file verification successful!')
120123

121-
# Hash the license key and fingerprint using SHA256
122-
digest = hashes.Hash(hashes.SHA256())
123-
digest.update(license_key.encode())
124-
digest.update(args.fingerprint.encode())
125-
key = digest.finalize()
124+
# Hash the license key and fingerprint using SHA256
125+
digest = hashes.Hash(hashes.SHA256())
126+
digest.update(license_key.encode())
127+
digest.update(args.fingerprint.encode())
128+
key = digest.finalize()
126129

127-
# Split and decode the enc value
128-
ciphertext, iv, tag = map(
129-
lambda p: base64.b64decode(p),
130-
enc.split('.'),
131-
)
132-
133-
# Decrypt ciphertext
134-
try:
135-
aes = Cipher(
136-
algorithms.AES(key),
137-
modes.GCM(iv, None, len(tag)),
138-
default_backend(),
130+
# Split and decode the enc value
131+
ciphertext, iv, tag = map(
132+
lambda p: base64.b64decode(p),
133+
enc.split('.'),
139134
)
140-
dec = aes.decryptor()
141135

142-
plaintext = dec.update(ciphertext) + dec.finalize_with_tag(tag)
143-
except (InvalidKey, InvalidTag):
144-
print('[error] machine file decryption failed!')
136+
# Decrypt ciphertext
137+
try:
138+
aes = Cipher(
139+
algorithms.AES(key),
140+
modes.GCM(iv, None, len(tag)),
141+
default_backend(),
142+
)
143+
dec = aes.decryptor()
144+
145+
plaintext = dec.update(ciphertext) + dec.finalize_with_tag(tag)
146+
except (InvalidKey, InvalidTag):
147+
print('[error] machine file decryption failed!')
145148

146149
return
147150

148-
print('[info] machine file decryption successful!')
149-
# print(json.dumps(json.loads(plaintext.decode()), indent=2)) # Uncomment to see the decrypted machine file
150-
except Exception as error:
151-
print("License verification failed, check your license: " + str(error))
152-
sys.exit(1)
151+
print('[info] machine file decryption successful!')
152+
# print(json.dumps(json.loads(plaintext.decode()), indent=2)) # Uncomment to see the decrypted machine file
153+
except Exception as error:
154+
print("License verification failed, check your license: " + str(error))
155+
return
156+
157+
print("Hello, World!")
158+
153159

154-
print("Hello, World!")
160+
if __name__ == '__main__':
161+
main()

0 commit comments

Comments
 (0)