| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- 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()
|