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.
--- 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