def edit_achievements(student_login): key = load_key(f'students/{student_login}.key') json_achievements = load_encrypted( f'students/{student_login}-achievements', key) with open(f'students/{student_login}.temp', 'w') as f: f.write(json_achievements) editor = os.getenv('EDITOR', default='vi') valid_json = False while not valid_json: os.system(f'{editor} students/{student_login}.temp') with open(f'students/{student_login}.temp') as f: json_achievements = f.read() try: _ = json.loads(json_achievements) valid_json = True except json.JSONDecodeError: print("Arquivo mal formatado.") time.sleep(2) save_encrypted(f'students/{student_login}-achievements', key, json_achievements) os.remove(f'students/{student_login}.temp')
def test_key(tmpdir): """ Test loading and saving of a key. """ key = 'ABC' path = tmpdir.mkdir('key').join('key1') utils.save_key(key, path) assert utils.load_key(path) == key
def parse_args(argv): """ Parse command line arguments Args: argv: Arguments from the command line. """ keystore_filename = 'keystore' port = 6666 signing_key = None dns = False try: opts, _ = getopt.getopt( argv[1:], 'hdnp=k=s=', ['help', 'debug', 'dns', 'port=', 'key=', 'store=']) for o, a in opts: if o in ('-h', '--help'): print('-d/--debug to enable debug prints') print('-n/--dns to start a dns chain') print('-p/--port to change default port') print('-k/--key to load a private key from a file') print('-s/--store to load a keystore from a file') sys.exit() if o in ('-d', '--debug'): set_debug() if o in ('-n', '--dns'): dns = True if o in ('-p', '--port'): try: port = int(a) except ValueError: print("Port was invalid (e.g. not an int)") elif o in ('-k', '--key'): try: signing_key = load_key(filename=a) print('Key successfully loaded') except Exception as e: print('Could not load Key / Key is invalid') print(e) elif o in ('-s', '--store'): keystore_filename = a print_debug_info(keystore_filename) except getopt.GetoptError as err: print('for help use --help') print(err) sys.exit() return keystore_filename, port, signing_key, dns
def select_userkey(self, describe: str): u_id = self.userkeydict[describe] prikey_t, pubkey_t = utils.get_userkey(u_id, self.database) for _ in range(5): input_window = InputWindow('密码 :', False) self.wait_window(input_window) status, prikey, pubkey = utils.load_key(pubkey_t, prikey_t, input_window.result) if not status: tkinter.messagebox.showwarning('Warning', '密码错误') continue self.prikey, self.pubkey = prikey, pubkey break if not status: tkinter.messagebox.showwarning('Warning', '密码五次输入错误,请重新选择') self.userkey_ls.delete(first='0', last='end')
def edit_achievements(student_login): key = load_key(f'students/{student_login}.key') json_achievements = load_encrypted( f'students/{student_login}-achievements', key) with open(f'students/{student_login}.temp', 'w') as f: f.write(json_achievements) editor = os.getenv('EDITOR', default='vi') os.system(f'{editor} students/{student_login}.temp') with open(f'students/{student_login}.temp') as f: json_achievements = f.read() save_encrypted(f'students/{student_login}-achievements', key, json_achievements) os.remove(f'students/{student_login}.temp')
def edit_achievements(student_login): key = load_key(f'students/{student_login}.key') json_achievements = load_encrypted( f'students/{student_login}-achievements', key) with open(f'students/{student_login}.temp', 'w') as f: f.write(json_achievements) if 'win32' in sys.platform: editor = os.getenv('EDITOR', default='notepad.exe') else: editor = os.getenv('EDITOR', default='vi') while True: os.system(f'{editor} students/{student_login}.temp') with open(f'students/{student_login}.temp') as f: json_achievements = f.read() try: _ = json.loads(json_achievements) except json.JSONDecodeError: print("Arquivo mal formatado.") time.sleep(2) continue print('Validando skills no arquivo JSON....') s = Student.load(student_login) s._load_skills_from_string(json_achievements) valid_skills = True for ach in s.all_achievements: try: ach.validate_metadata() except ValueError as e: print('- ', ach, '\n\t', e) valid_skills = False if valid_skills: break time.sleep(2) print('\n\n==================') print('Nenhum erro encontrado!') save_encrypted(f'students/{student_login}-achievements', key, json_achievements) os.remove(f'students/{student_login}.temp')
def select_thirdkey(self, describe): u_id = self.thirdkeydict[describe] _, self.thirdkey, _ = utils.load_key( utils.get_thirdkey(u_id, self.database))
def on_receive_message_1(client, userdata, msg): """ Handles the registration request coming from a device. Check the algorithms specified by this device and proceed as indicated to negotiate a shared key (HAND-SHAKE). It builds the message 2 to be sent to the device with the ephemeral public key of the platform for the DH or ECDH process. """ global encriptor, shared_key global asymmetricAlgorithm, symmetricAlgorithm # Received message 1 for the registration process auth = msg.get("auth", "") if auth != "": asymmetricAlgorithm = auth.get("asymmetric", "") if asymmetricAlgorithm == "dh": g = auth.get("g", "") p = auth.get("p", "") device_pub_key = auth.get("public_key", "") if g == "" and p == "" and device_pub_key == "": # Cannot be empty. return False # Generate private key and public key for platform # in the registration process pn = dh.DHParameterNumbers(p, g) parameters = pn.parameters(default_backend()) private_key, public_key = utils.dhGenKeys(parameters) # Generate shared key device_pub_key = utils.load_key(device_pub_key) shared_key = utils.dhGenSharedKey(private_key, device_pub_key) # Building message two. answer_registration_request = { "auth": { "public_key": public_key.public_bytes( Encoding.PEM,\ PublicFormat.SubjectPublicKeyInfo ).decode( "utf-8" ) } } elif asymmetricAlgorithm == "ecdh": x = auth.get("x", "") y = auth.get("y", "") if x == "" and y == "": # Cannot be empty. return False private_key, public_key = utils.ecdhGenKeys(utils.curve) device_pub_key = ec.Point(utils.curve, x, y) shared_key = utils.ecdhGenSharedKey(private_key, device_pub_key) # Building message two. answer_registration_request = { "auth": { "x": public_key.x, "y": public_key.y } } if shared_key == "": # Cannot be empty. return False symmetricAlgorithm = auth.get("symmetric", "") if symmetricAlgorithm == "fernet": encriptor = Fernet(base64.urlsafe_b64encode(shared_key)) elif symmetricAlgorithm == "chacha": encriptor = ChaCha20Poly1305(shared_key) message = add_header_message( answer_registration_request, userdata,\ REGISTRATION_TOPIC, 2 ) utils.send(client, None, message) return True return False