-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
37d2d3b
commit aafaf57
Showing
2 changed files
with
59 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import unittest | ||
import base64 | ||
|
||
from Crypto.Cipher import AES | ||
|
||
from encrypt_content import ( | ||
hash_md5, | ||
encrypt_text_aes | ||
) | ||
|
||
|
||
class TestEncryptContent(unittest.TestCase): | ||
|
||
def test_hash_md5(self): | ||
""" | ||
Verify that md5 hashes are being computed correctly. | ||
""" | ||
md5_bytes = hash_md5('test') | ||
self.assertEqual(md5_bytes.encode('hex'), '098f6bcd4621d373cade4e832627b4f6') | ||
|
||
def test_encrypt_text_aes(self): | ||
""" | ||
Verify that the ciphertext is actually decryptable. | ||
Note that just because this passes doesn't mean everything is necessarily | ||
working, as in production the actual decryption is done in JavaScript. | ||
""" | ||
text = 'some text to encrypt' | ||
password = 'agoodpassword' | ||
|
||
iv_b64, ciphertext_b64, padding_char = encrypt_text_aes(text, password) | ||
|
||
cipher = AES.new(hash_md5(password), AES.MODE_CBC, base64.b64decode(iv_b64)) | ||
plaintext = cipher.decrypt(base64.b64decode(ciphertext_b64)) | ||
|
||
self.assertEqual(plaintext.decode('utf-8').rstrip(padding_char), text) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |