Skip to content

Commit

Permalink
age encryption support #49 exception handling code
Browse files Browse the repository at this point in the history
  • Loading branch information
clach04 committed Nov 26, 2024
1 parent 470a330 commit c7af869
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions puren_tonbo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,9 +658,18 @@ def read_from(self, file_object):
if not isinstance(password, bytes):
password = password.decode("utf-8")

identities = [age.keys.password.PasswordKey(password)]
with age.file.Decryptor(identities, file_object) as decryptor:
plaintext = decryptor.read()
try:
identities = [age.keys.password.PasswordKey(password)]
with age.file.Decryptor(identities, file_object) as decryptor:
plaintext = decryptor.read()
except age.exceptions.NoIdentity as info:
# probbaly NoIdentity("No matching key")
raise BadPassword(info) # FIXME BadPassword(BadPassword("for 'file-like-object'",),)
except Exception as info:
#raise # DEBUG
# TODO chain exception...
#raise PurenTonboException(info.message)
raise PurenTonboException(info)

return plaintext

Expand All @@ -671,9 +680,15 @@ def write_to(self, file_object, byte_data):
if not isinstance(password, bytes):
password = password.decode("utf-8")

identities = [age.keys.password.PasswordKey(password)]
with age.file.Encryptor(identities, file_object) as encryptor:
encryptor.write(byte_data)
try:
identities = [age.keys.password.PasswordKey(password)]
with age.file.Encryptor(identities, file_object) as encryptor:
encryptor.write(byte_data)
except Exception as info:
#raise # DEBUG
# TODO chain exception...
#raise PurenTonboException(info.message)
raise PurenTonboException(info)

# TODO AE-2 (no CRC), otherwise the same as AE-1 - see https://github.com/clach04/puren_tonbo/wiki/zip-format
class ZipEncryptedFileBase(EncryptedFile):
Expand Down

0 comments on commit c7af869

Please sign in to comment.