make linter happy

master
Felix Pankratz 6 months ago
parent 4727f389ed
commit 3b4939f9dc

@ -1,43 +1,48 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import argparse
import datetime
import hashlib
import os
import random import random
import secrets import secrets
import os import string
import sys import sys
import hashlib
DEFAULT_SECRET_PATH = 'secret' DEFAULT_SECRET_PATH = 'secret'
def create_secret(): def create_secret():
"""returns 128 bytes of randomness as hex bytes"""
return secrets.token_hex(128) return secrets.token_hex(128)
def generate_password(): def generate_password():
import string """returns a random 24 char password"""
alphabet = string.ascii_letters + string.digits + '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}' alphabet = string.ascii_letters + string.digits + '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}'
return ''.join(random.choice(alphabet) for i in range(24)) return ''.join(random.choice(alphabet) for i in range(24))
def month_timestamp(): def month_timestamp():
import datetime """returns the unix timestamp of the first of the current month"""
today = datetime.datetime.now() today = datetime.datetime.now()
start_of_month = datetime.datetime(today.year, today.month, 1, 0, 0, 0, 0) start_of_month = datetime.datetime(today.year, today.month, 1, 0, 0, 0, 0)
return int(start_of_month.timestamp()) return int(start_of_month.timestamp())
def generate_seed(secret): def generate_seed(secret):
m = hashlib.sha256() """returns an rng seed based on the current date"""
m.update(bytes.fromhex(secret)) hash_object = hashlib.sha256()
m.update(str(month_timestamp()).encode()) hash_object.update(bytes.fromhex(secret))
return m.digest() hash_object.update(str(month_timestamp()).encode())
return hash_object.digest()
def generate_secret(secret_path): def generate_secret(secret_path):
"""generates a secret and writes it as a file"""
print('Generating a new secret... ', end='') print('Generating a new secret... ', end='')
secret = create_secret() secret = create_secret()
with open(secret_path, 'w') as f: with open(secret_path, 'w') as file:
f.write(secret) file.write(secret)
print('done. Send this to the other party:') print('done. Send this to the other party:')
print(secret) print(secret)
def main(): def main():
import argparse
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
prog='Remote Admin Password Solution', prog='Remote Admin Password Solution',
description='Generate rotating passwords based on a shared secret' description='Generate rotating passwords based on a shared secret'
@ -47,7 +52,10 @@ def main():
args = parser.parse_args() args = parser.parse_args()
secret_path = args.secret if args.secret else DEFAULT_SECRET_PATH if args.secret:
secret_path = os.path.realpath(args.secret)
else:
secret_path = os.path.realpath(DEFAULT_SECRET_PATH)
secret = '' secret = ''
if args.new_secret: if args.new_secret:
@ -65,6 +73,7 @@ def main():
except FileNotFoundError: except FileNotFoundError:
print(f'ERROR: Secret file {secret_path} could not be found.') print(f'ERROR: Secret file {secret_path} could not be found.')
sys.exit(1) sys.exit(1)
print(f'Secret {secret_path} loaded.') print(f'Secret {secret_path} loaded.')
random.seed(generate_seed(secret)) random.seed(generate_seed(secret))

Loading…
Cancel
Save