Coverage for src/tests/test_crypto.py: 100%

42 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-06-01 15:07 +0000

1import pytest 

2 

3from couchers import crypto 

4from tests.test_fixtures import testconfig # noqa 

5 

6 

7@pytest.fixture(autouse=True) 

8def _(testconfig): 

9 pass 

10 

11 

12def test_b64(): 

13 assert crypto.b64decode(crypto.b64encode(b"hello there")) == b"hello there" 

14 

15 

16def test_simple_crypto(): 

17 assert crypto.simple_decrypt("test_simple", crypto.simple_encrypt("test_simple", b"hello there")) == b"hello there" 

18 

19 

20def test_hash_sigs(): 

21 sig = crypto.generate_hash_signature(b"this is the message", crypto.get_secret("test_hash")) 

22 crypto.verify_hash_signature(b"this is the message", crypto.get_secret("test_hash"), sig) 

23 

24 

25def test_asym_crypto(): 

26 skey, pkey = crypto.generate_asym_keypair() 

27 encrypted = crypto.asym_encrypt(pkey, b"a very secret message") 

28 assert crypto.asym_decrypt(skey, encrypted) == b"a very secret message" 

29 

30 

31def test_stable_secure_uniform(): 

32 # make sure it didn't change 

33 assert crypto.stable_secure_uniform(key=b"stable", seed=b"seed0") == 0.17992286217826525 

34 assert crypto.stable_secure_uniform(key=b"stable", seed=b"seed1") == 0.725282807072193 

35 assert crypto.stable_secure_uniform(key=b"stable", seed=b"seed2") == 0.9063440288190295 

36 assert crypto.stable_secure_uniform(key=b"stable", seed=b"seed3") == 0.6327659823819931 

37 assert crypto.stable_secure_uniform(key=b"stable", seed=b"seed4") == 0.927720188949493 

38 assert crypto.stable_secure_uniform(key=b"stable", seed=b"seed5") == 0.055950106064694194 

39 assert crypto.stable_secure_uniform(key=b"stable", seed=b"seed6") == 0.5282629474672513 

40 assert crypto.stable_secure_uniform(key=b"stable", seed=b"seed7") == 0.8330914059728719 

41 assert crypto.stable_secure_uniform(key=b"stable", seed=b"seed8") == 0.8089643245604919 

42 assert crypto.stable_secure_uniform(key=b"stable", seed=b"seed9") == 0.4034213734044777 

43 

44 # make sure it's rand unif 

45 for _ in range(1000): 

46 u = crypto.stable_secure_uniform(key=b"test", seed=crypto.random_bytes(32)) 

47 assert u > 0 and u < 1 

48 print(u) 

49 

50 # make sure it's stable 

51 u1 = crypto.stable_secure_uniform(key=b"test", seed=b"seed1") 

52 u2 = crypto.stable_secure_uniform(key=b"test", seed=b"seed1") 

53 u3 = crypto.stable_secure_uniform(key=b"test", seed=b"seed1") 

54 assert u1 == u2 and u2 == u3 

55 

56 # make sure it's diff 

57 u4 = crypto.stable_secure_uniform(key=b"test", seed=b"seed2") 

58 u5 = crypto.stable_secure_uniform(key=b"test", seed=b"seed3") 

59 assert u4 != u5 

60 

61 u6 = crypto.stable_secure_uniform(key=b"test1", seed=b"seed") 

62 u7 = crypto.stable_secure_uniform(key=b"test2", seed=b"seed") 

63 assert u6 != u7