Skip to content

Commit

Permalink
Dedup sending age response
Browse files Browse the repository at this point in the history
  • Loading branch information
romanz committed Dec 25, 2024
1 parent d005bad commit e8e033f
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions libagent/age/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,30 +121,33 @@ def run_decrypt(device_type, args):
for file_index, stanzas in stanza_map.items():
_handle_single_file(file_index, stanzas, identities, c)

sys.stdout.buffer.write('-> done\n\n'.encode())
sys.stdout.flush()
sys.stdout.close()
send('-> done\n\n')


def _handle_single_file(file_index, stanzas, identities, c):
d = c.device.__class__.__name__
for peer_pubkey, encrypted in stanzas:
for identity in identities:
id_str = identity.to_string()
msg = base64_encode(f'Please confirm {id_str} decryption on {d} device...'.encode())
sys.stdout.buffer.write(f'-> msg\n{msg}\n'.encode())
sys.stdout.flush()
msg = f'Please confirm {id_str} decryption on {d} device...'
send(f'-> msg\n{base64_encode(msg.encode())}\n')

key = c.ecdh(identity=identity, peer_pubkey=peer_pubkey)

result = decrypt(key=key, encrypted=encrypted)
if not result:
continue

sys.stdout.buffer.write(f'-> file-key {file_index}\n{base64_encode(result)}\n'.encode())
sys.stdout.flush()
send(f'-> file-key {file_index}\n{base64_encode(result)}\n')
return


def send(msg):
"""Send a response back to `age` binary."""
sys.stdout.buffer.write(msg.encode())
sys.stdout.flush()


def main(device_type):
"""Parse command-line arguments."""
p = argparse.ArgumentParser()
Expand Down

0 comments on commit e8e033f

Please sign in to comment.