|
| 1 | +import os, time, sys, pickle, os |
| 2 | +from pproxy.cipher import MAP |
| 3 | +from pproxy.cipherpy import MAP as MAP_PY |
| 4 | + |
| 5 | +def test_both_cipher(A, B, size=4*1024, repeat=16): |
| 6 | + print('Testing', B.__name__, '...') |
| 7 | + t1 = t2 = 0 |
| 8 | + for i in range(repeat): |
| 9 | + assert A.KEY_LENGTH == B.KEY_LENGTH and A.IV_LENGTH == B.IV_LENGTH |
| 10 | + key = os.urandom(A.KEY_LENGTH) |
| 11 | + iv = os.urandom(A.IV_LENGTH) |
| 12 | + t = time.perf_counter() |
| 13 | + a = A(key) |
| 14 | + a.setup_iv(iv) |
| 15 | + t1 += time.perf_counter() - t |
| 16 | + t = time.perf_counter() |
| 17 | + b = B(key) |
| 18 | + b.setup_iv(iv) |
| 19 | + t2 += time.perf_counter() - t |
| 20 | + s = os.urandom(size) |
| 21 | + t = time.perf_counter() |
| 22 | + s2 = a.encrypt(s) |
| 23 | + t1 += time.perf_counter() - t |
| 24 | + t = time.perf_counter() |
| 25 | + s3 = b.encrypt(s) |
| 26 | + t2 += time.perf_counter() - t |
| 27 | + assert s2 == s3 |
| 28 | + |
| 29 | + t = time.perf_counter() |
| 30 | + a = A(key, True) |
| 31 | + a.setup_iv(iv) |
| 32 | + t1 += time.perf_counter() - t |
| 33 | + t = time.perf_counter() |
| 34 | + b = B(key, True) |
| 35 | + b.setup_iv(iv) |
| 36 | + t2 += time.perf_counter() - t |
| 37 | + t = time.perf_counter() |
| 38 | + s4 = a.decrypt(s2) |
| 39 | + t1 += time.perf_counter() - t |
| 40 | + t = time.perf_counter() |
| 41 | + s5 = b.decrypt(s2) |
| 42 | + t2 += time.perf_counter() - t |
| 43 | + assert s4 == s5 == s |
| 44 | + |
| 45 | + print('Passed', t1, t2) |
| 46 | + |
| 47 | +def test_cipher(A, data, size=4*1024, repeat=16): |
| 48 | + if A.__name__ not in data: |
| 49 | + if input('Correct now? (Y/n)').upper() != 'Y': |
| 50 | + return |
| 51 | + d = [] |
| 52 | + for i in range(repeat): |
| 53 | + key = os.urandom(A.KEY_LENGTH) |
| 54 | + iv = os.urandom(A.IV_LENGTH) |
| 55 | + a = A(key) |
| 56 | + a.setup_iv(iv) |
| 57 | + s = os.urandom(size) |
| 58 | + s2 = a.encrypt(s) |
| 59 | + a = A(key, True) |
| 60 | + a.setup_iv(iv) |
| 61 | + s4 = a.decrypt(s2) |
| 62 | + assert s == s4 |
| 63 | + d.append((key, iv, s, s2)) |
| 64 | + data[A.__name__] = d |
| 65 | + print('Saved correct data') |
| 66 | + else: |
| 67 | + t = time.perf_counter() |
| 68 | + print('Testing', A.__name__, '...') |
| 69 | + for key, iv, s, s2 in data[A.__name__]: |
| 70 | + a = A(key) |
| 71 | + a.setup_iv(iv) |
| 72 | + s3 = a.encrypt(s) |
| 73 | + assert s2 == s3 |
| 74 | + a = A(key, True) |
| 75 | + a.setup_iv(iv) |
| 76 | + s4 = a.decrypt(s2) |
| 77 | + assert s == s4 |
| 78 | + print('Passed', time.perf_counter()-t) |
| 79 | + |
| 80 | + |
| 81 | +cipher = sys.argv[1] if len(sys.argv) > 1 else None |
| 82 | +data = pickle.load(open('.cipherdata', 'rb')) if os.path.exists('.cipherdata') else {} |
| 83 | + |
| 84 | +if cipher is None: |
| 85 | + print('Testing all ciphers') |
| 86 | + |
| 87 | + for cipher, B in sorted(MAP_PY.items()): |
| 88 | + A = MAP.get(cipher) |
| 89 | + if A: |
| 90 | + test_both_cipher(A, B) |
| 91 | + elif B.__name__ in data: |
| 92 | + test_cipher(B, data) |
| 93 | +else: |
| 94 | + B = MAP_PY[cipher] |
| 95 | + A = MAP.get(cipher) |
| 96 | + if A: |
| 97 | + test_both_cipher(A, B) |
| 98 | + else: |
| 99 | + test_cipher(B, data) |
| 100 | + |
| 101 | + |
| 102 | +pickle.dump(data, open('.cipherdata', 'wb')) |
| 103 | + |
0 commit comments