-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
test_crypt.py
64 lines (47 loc) · 1.7 KB
/
test_crypt.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
import pytest
from coincurve import PrivateKey
from eth_keys import keys
from ecies import ECIES_CONFIG, decrypt, encrypt
from ecies.utils import generate_eth_key, generate_key
from ecies.utils.hex import decode_hex
data = b"this is a test"
def __check(k, compressed=False):
sk_hex = k.to_hex()
if isinstance(k, PrivateKey):
pk_hex = k.public_key.format(compressed).hex()
elif isinstance(k, keys.PrivateKey):
pk_hex = k.public_key.to_hex()
else:
raise NotImplementedError
assert data == decrypt(sk_hex, encrypt(pk_hex, data))
sk_bytes = decode_hex(sk_hex)
pk_bytes = decode_hex(pk_hex)
if len(pk_bytes) == 64: # eth
pk_bytes = b"\x04" + pk_bytes
assert data == decrypt(sk_bytes, encrypt(pk_bytes, data))
def test_elliptic():
__check(generate_eth_key())
__check(generate_key())
__check(generate_key(), True)
with pytest.raises(TypeError):
encrypt(1, data)
k = generate_key()
pk_hex = k.public_key.format(True).hex()
with pytest.raises(TypeError):
decrypt(1, encrypt(bytes.fromhex(pk_hex), data))
def test_hkdf_config():
ECIES_CONFIG.is_hkdf_key_compressed = True
__check(generate_key())
ECIES_CONFIG.is_hkdf_key_compressed = False
def test_ephemeral_key_config():
ECIES_CONFIG.is_ephemeral_key_compressed = True
__check(generate_key())
ECIES_CONFIG.is_ephemeral_key_compressed = False
def test_aes_nonce_config():
ECIES_CONFIG.symmetric_nonce_length = 12
__check(generate_key())
ECIES_CONFIG.symmetric_nonce_length = 16
def test_sym_config():
ECIES_CONFIG.symmetric_algorithm = "xchacha20"
__check(generate_key())
ECIES_CONFIG.symmetric_algorithm = "aes-256-gcm"