提交
This commit is contained in:
1 parent
d336fd0226
commit
2e9d020d50
3454 files changed
+878963
-319
No files matched your search
@@ -0,0 +1,50 @@
|
||||
import unittest
|
||||
|
||||
|
||||
from app.core import api_crypto
|
||||
|
||||
|
||||
class ApiCryptoTests(unittest.TestCase):
|
||||
def test_derive_key_is_stable_for_same_input(self):
|
||||
key1 = api_crypto.derive_key(
|
||||
fixed_secret="fixed-secret",
|
||||
user_id="u1",
|
||||
ts=1714300000000,
|
||||
nonce="nonce-1",
|
||||
)
|
||||
key2 = api_crypto.derive_key(
|
||||
fixed_secret="fixed-secret",
|
||||
user_id="u1",
|
||||
ts=1714300000000,
|
||||
nonce="nonce-1",
|
||||
)
|
||||
self.assertEqual(key1, key2)
|
||||
self.assertEqual(len(key1), 32)
|
||||
|
||||
def test_encrypt_decrypt_roundtrip(self):
|
||||
key = api_crypto.derive_key(
|
||||
fixed_secret="fixed-secret",
|
||||
user_id="u1",
|
||||
ts=1714300000000,
|
||||
nonce="nonce-1",
|
||||
)
|
||||
payload = {"a": 1, "b": "x"}
|
||||
cipher = api_crypto.encrypt_json(payload, key)
|
||||
plain = api_crypto.decrypt_json(cipher, key)
|
||||
self.assertEqual(plain, payload)
|
||||
|
||||
def test_encrypt_decrypt_roundtrip_cbc(self):
|
||||
key = api_crypto.derive_key(
|
||||
fixed_secret="fixed-secret",
|
||||
user_id="u1",
|
||||
ts=1714300000000,
|
||||
nonce="nonce-2",
|
||||
)
|
||||
payload = {"list": [1, 2, 3], "ok": True}
|
||||
cipher = api_crypto.encrypt_json(payload, key, alg="cbc")
|
||||
plain = api_crypto.decrypt_json(cipher, key, alg="cbc")
|
||||
self.assertEqual(plain, payload)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user