Skip to content

Commit

Permalink
Complete U001 support and testing #4
Browse files Browse the repository at this point in the history
  • Loading branch information
clach04 committed Nov 13, 2024
1 parent 485acb1 commit 4b0e007
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
4 changes: 4 additions & 0 deletions jenc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ def encrypt(password, plaintext_bytes, jenc_version='V001'):
log.debug('password %r', password)
if this_file_meta['keyFactory'] == JENC_PBKDF2WithHmacSHA512:
derived_key = PBKDF2(password, salt_bytes, this_file_meta['keyLength'] // 8, count=this_file_meta['keyIterationCount'], hmac_hash_module=SHA512)
elif this_file_meta['keyFactory'] == JENC_PBKDF2WithHmacSHA1:
derived_key = PBKDF2(password, salt_bytes, this_file_meta['keyLength'] // 8, count=this_file_meta['keyIterationCount'], hmac_hash_module=SHA1)
else:
raise UnsupportedMetaData('keyFactory %r' % this_file_meta['keyFactory'])
log.debug('derived_key %r', derived_key)
Expand Down Expand Up @@ -319,6 +321,8 @@ def decrypt_file_handle(file_object, password):
log.debug('password %r', password)
if this_file_meta['keyFactory'] == JENC_PBKDF2WithHmacSHA512:
derived_key = PBKDF2(password, salt_bytes, this_file_meta['keyLength'] // 8, count=this_file_meta['keyIterationCount'], hmac_hash_module=SHA512)
elif this_file_meta['keyFactory'] == JENC_PBKDF2WithHmacSHA1:
derived_key = PBKDF2(password, salt_bytes, this_file_meta['keyLength'] // 8, count=this_file_meta['keyIterationCount'], hmac_hash_module=SHA1)
else:
raise UnsupportedMetaData('keyFactory %r' % this_file_meta['keyFactory'])
log.debug('derived_key %r', derived_key)
Expand Down
47 changes: 40 additions & 7 deletions jenc/tests/testsuite.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,33 +60,66 @@ def skip(self, reason):
"""

class TestJenc(TestUtil):
def check_get_what_you_put_in(self, original_plaintext, password):
encrypted_bytes = jenc.encrypt(password, original_plaintext)
def check_get_what_you_put_in(self, original_plaintext, password, version=None):
if version:
encrypted_bytes = jenc.encrypt(password, original_plaintext, jenc_version=version)
self.assertEqual(version.encode('us-ascii'), encrypted_bytes[:4])
else:
encrypted_bytes = jenc.encrypt(password, original_plaintext)
plaintext_bytes = jenc.decrypt(password, encrypted_bytes)

self.assertEqual(plaintext_bytes, original_plaintext)

def check_same_input_different_crypted_text(self, original_plaintext, password):
encrypted_bytes1 = jenc.encrypt(password, original_plaintext)
def check_same_input_different_crypted_text(self, original_plaintext, password, version=None):
if version:
encrypted_bytes1 = jenc.encrypt(password, original_plaintext, jenc_version=version)
self.assertEqual(version.encode('us-ascii'), encrypted_bytes1[:4])
else:
encrypted_bytes1 = jenc.encrypt(password, original_plaintext)
plaintext_bytes1 = jenc.decrypt(password, encrypted_bytes1)

encrypted_bytes2 = jenc.encrypt(password, original_plaintext)
if version:
encrypted_bytes2 = jenc.encrypt(password, original_plaintext, jenc_version=version)
self.assertEqual(version.encode('us-ascii'), encrypted_bytes2[:4])
else:
encrypted_bytes2 = jenc.encrypt(password, original_plaintext)
plaintext_bytes2 = jenc.decrypt(password, encrypted_bytes2)

self.assertEqual(original_plaintext, plaintext_bytes1)
self.assertEqual(original_plaintext, plaintext_bytes2)
self.assertNotEqual(encrypted_bytes1, encrypted_bytes2)

def test_hello_world_enc_dec(self):
def test_hello_world_enc_dec_default_encryption(self):
password = 'geheim' # same password used in demos for Java version https://github.com/opensource21/jpencconverter/tree/master/src/test/encrypted
original_plaintext = b"Hello World"
self.check_get_what_you_put_in(original_plaintext, password)

def test_hello_world_encs_different_each_time(self):
def test_hello_world_encs_different_each_time_encryption(self):
password = 'geheim' # same password used in demos for Java version https://github.com/opensource21/jpencconverter/tree/master/src/test/encrypted
original_plaintext = b"Hello World"
self.check_same_input_different_crypted_text(original_plaintext, password)

def test_hello_world_enc_dec_default_v001(self):
password = 'geheim' # same password used in demos for Java version https://github.com/opensource21/jpencconverter/tree/master/src/test/encrypted
original_plaintext = b"Hello World"
self.check_get_what_you_put_in(original_plaintext, password, version='V001')

def test_hello_world_encs_different_each_time_v001(self):
password = 'geheim' # same password used in demos for Java version https://github.com/opensource21/jpencconverter/tree/master/src/test/encrypted
original_plaintext = b"Hello World"
self.check_same_input_different_crypted_text(original_plaintext, password, version='V001')

def test_hello_world_enc_dec_default_u001(self):
password = 'geheim' # same password used in demos for Java version https://github.com/opensource21/jpencconverter/tree/master/src/test/encrypted
original_plaintext = b"Hello World"
self.check_get_what_you_put_in(original_plaintext, password, version='U001')

def test_hello_world_encs_different_each_time_u001(self):
password = 'geheim' # same password used in demos for Java version https://github.com/opensource21/jpencconverter/tree/master/src/test/encrypted
original_plaintext = b"Hello World"
self.check_same_input_different_crypted_text(original_plaintext, password, version='U001')


class TestJencFiles(TestUtil):
data_folder = os.path.join(
os.path.dirname(jenc.tests.__file__),
Expand Down

0 comments on commit 4b0e007

Please sign in to comment.