-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
69 lines (53 loc) · 1.88 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import os
import time
import traceback
from hxcrypto import Encryptor, is_aead, method_supported, AEncryptor
BLOCK = 10240
REPEAT = 1024
def test_one(method, block, repeat):
data = os.urandom(block)
if is_aead(method):
cipher = AEncryptor(b'cXdlcg==', method, b"ctx", check_iv=False)
cipher1 = AEncryptor(b'cXdlcg==', method, b"ctx", check_iv=False)
else:
cipher = Encryptor('123456', method, check_iv=False)
cipher1 = Encryptor('123456', method, check_iv=False)
ct1 = cipher.encrypt(data)
cipher1.decrypt(ct1)
time_log = time.time()
for _ in range(repeat):
ct1 = cipher.encrypt(data)
ct2 = cipher.encrypt(data)
cipher1.decrypt(ct1)
cipher1.decrypt(ct2)
return time.time() - time_log
def test():
size = (BLOCK / 1024) * (REPEAT / 1024) * 2
print('encrypt and decrypt %dMB data.' % size)
lst = sorted(method_supported.keys())
lst = sorted(lst, key=lambda method: is_aead(method))
size = max([len(x) for x in lst])
format_ = '%-{:d}s %.4fs'.format(size)
for method in lst:
try:
time_used = test_one(method, BLOCK, REPEAT)
print(format_ % (method, time_used))
except Exception as e:
print(traceback.format_exc())
def test_for_result():
result = []
size = (BLOCK / 1024) * (REPEAT / 1024) * 2
result.append('encrypt and decrypt %dMB data.' % size)
lst = sorted(method_supported.keys())
lst = sorted(lst, key=lambda method: is_aead(method))
size = max([len(x) for x in lst])
format_ = '%-{:d}s %.4fs'.format(size)
for method in lst:
try:
time_used = test_one(method, BLOCK, REPEAT)
result.append(format_ % (method, time_used))
except Exception as e:
result.append(format_ % method)
return '\n'.join(result)
if __name__ == '__main__':
print(test())