Make crypto module python3-compatible 3.26
authorDenis Laxalde <denis.laxalde@logilab.fr>
Thu, 16 May 2019 17:17:42 +0200
branch3.26
changeset 12615 7abe23cbfda1
parent 12605 bd6ba326765e
child 12616 bfab695b740a
Make crypto module python3-compatible * Remove usage of unicode() and decode the base64-encoded string in encrypt(); * Encode the string received in decrypt() as (I supposed) it should come from the result of encrypt(). Add tests for this module along the way.
cubicweb/crypto.py
cubicweb/test/unittest_crypto.py
requirements/test-misc.txt
--- a/cubicweb/crypto.py	Thu May 16 01:23:51 2019 +0200
+++ b/cubicweb/crypto.py	Thu May 16 17:17:42 2019 +0200
@@ -36,12 +36,13 @@
 
 def encrypt(data, seed):
     string = pickle.dumps(data)
-    string = string + '*' * (8 - len(string) % 8)
+    string = string + b'*' * (8 - len(string) % 8)
     string = b64encode(_cypherer(seed).encrypt(string))
-    return unicode(string)
+    return string.decode('utf-8')
 
 
 def decrypt(string, seed):
+    string = string.encode('utf-8')
     # pickle ignores trailing characters so we do not need to strip them off
     string = _cypherer(seed).decrypt(b64decode(string))
     return pickle.loads(string)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cubicweb/test/unittest_crypto.py	Thu May 16 17:17:42 2019 +0200
@@ -0,0 +1,18 @@
+from unittest import TestCase
+
+from cubicweb import crypto
+
+
+class CryptoTC(TestCase):
+
+    def test_encrypt_decrypt_roundtrip(self):
+        data = {'a': u'ah', 'b': [1, 2]}
+        seed = 'ssss'
+        crypted = crypto.encrypt(data, seed)
+        decrypted = crypto.decrypt(crypted, seed)
+        self.assertEqual(decrypted, data)
+
+
+if __name__ == '__main__':
+    import unittest
+    unittest.main()
--- a/requirements/test-misc.txt	Thu May 16 01:23:51 2019 +0200
+++ b/requirements/test-misc.txt	Thu May 16 17:17:42 2019 +0200
@@ -8,6 +8,7 @@
 
 ## cubicweb/test
 Pygments
+pycrypto
 mock
 #fyzz XXX pip install fails
 cubicweb-file == 1.18.0