Skip to content

Commit

Permalink
jenc support for U001 and V001 as output formats #169
Browse files Browse the repository at this point in the history
  • Loading branch information
clach04 committed Nov 17, 2024
1 parent 20a3c83 commit ab555eb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
30 changes: 26 additions & 4 deletions puren_tonbo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ class Jenc(EncryptedFile):
extensions = [
'.jenc', # md and txt?
]
_jenc_version = None # use default (latest)

def read_from(self, file_object):
# TODO catch exceptions and raise PurenTonboException()
Expand All @@ -560,9 +561,28 @@ def write_to(self, file_object, byte_data):
if not isinstance(password, bytes):
password = password.decode("utf-8")

crypted_bytes = jenc.encrypt(password, byte_data)
crypted_bytes = jenc.encrypt(password, byte_data, jenc_version=self._jenc_version)
file_object.write(crypted_bytes)

class JencU001(Jenc):
description = 'Markor / jpencconverter U001 PBKDF2WithHmacSHA1 iterations 10000 AES-256-GCM'
extensions = [
'.u001.jenc', # md and txt?
'.u001_jenc', # md and txt?
# Do NOT include generic .jenc
]
_jenc_version = 'U001' # FIXME constant from jenc instead of literal

class JencV001(Jenc):
description = 'Markor / jpencconverter pbkdf2-hmac-sha512 iterations 10000 AES-256-GCM'
extensions = [
'.v001.jenc', # md and txt?
'.v001_jenc', # md and txt?
# Do NOT include generic .jenc
]
_jenc_version = None # use default (latest)


class TomboBlowfish(EncryptedFile):
"""Read/write Tombo (modified) Blowfish encrypted files
Compatible with files in:
Expand Down Expand Up @@ -728,8 +748,9 @@ class ZipBzip2AES(ZipAES):
file_type_handlers[file_extension] = enc_class

if jenc: # FIXME, handle this via introspection, see code above for RawFile
for file_extension in Jenc.extensions:
file_type_handlers[file_extension] = Jenc
for enc_class in (JencV001, JencU001, Jenc): # order significant for filename extension lookup
for file_extension in enc_class.extensions:
file_type_handlers[file_extension] = enc_class

if chi_io:
for file_extension in TomboBlowfish.extensions:
Expand Down Expand Up @@ -767,6 +788,7 @@ class ZipBzip2AES(ZipAES):

def filename2handler(filename, default_handler=None):
filename = filename.lower()
# check for extensions with multiple periods/dots/fullstops
if filename.endswith('.aes256.zip'):
file_extn = '.aes.zip'
elif filename.endswith('.aes.zip'):
Expand All @@ -780,7 +802,7 @@ def filename2handler(filename, default_handler=None):
elif filename.endswith('.oldstored.zip'):
file_extn = '.oldstored.zip'
else:
_dummy, file_extn = os.path.splitext(filename)
_dummy, file_extn = os.path.splitext(filename) # this is very agressive, splits on most-right-hand side (maybe want cipher name lookup ASWELL as filename extension)
log.debug('clach04 DEBUG file_extn: %r', file_extn)
log.debug('clach04 DEBUG file_type_handlers: %r', file_type_handlers)
handler_class = file_type_handlers.get(file_extn) or default_handler
Expand Down
7 changes: 7 additions & 0 deletions puren_tonbo/tools/ptcipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ def file_replace(src, dst):
ptcipher --cipher jenc --encrypt -p password README.md -o README.md.jenc
# NOTE underscore in cipher
ptcipher --cipher .u001_jenc --encrypt -p password README.md -o README.md.jenc
ptcipher --cipher .u001_jenc --encrypt -p password README.md -o README.md.u001.jenc
ptcipher --encrypt -p password README.md -o README.md.u001.jenc # DOES NOT YET WORK - https://github.com/clach04/puren_tonbo/issues/171
ptcipher --encrypt -p password README.md -o README.md.jenc
"""

def main(argv=None):
Expand Down Expand Up @@ -163,6 +169,7 @@ def usage():
password = password.encode('us-ascii')

if options.cipher:
#import pdb ; pdb.set_trace()
handler_class = puren_tonbo.filename2handler('_.' + options.cipher) # TODO options.cipher to filename extension is less than ideal
else:
handler_class = None
Expand Down

0 comments on commit ab555eb

Please sign in to comment.