# HG changeset patch # User Denis Laxalde # Date 1558019862 -7200 # Node ID 7abe23cbfda160f256235ed76503ef2d8777a849 # Parent bd6ba326765ef0b7a89b34f97b4859bd290aac82 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. diff -r bd6ba326765e -r 7abe23cbfda1 cubicweb/crypto.py --- 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) diff -r bd6ba326765e -r 7abe23cbfda1 cubicweb/test/unittest_crypto.py --- /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() diff -r bd6ba326765e -r 7abe23cbfda1 requirements/test-misc.txt --- 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