-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_crypto_utils.py
More file actions
52 lines (34 loc) · 1.42 KB
/
test_crypto_utils.py
File metadata and controls
52 lines (34 loc) · 1.42 KB
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
"""Tests for utils/crypto_utils.py"""
import pytest
from utils.crypto_utils import hmac_sha256, sha256, md5, generate_nonce, timestamp_nonce
def test_hmac_sha256_known_value():
# Known reference: echo -n "symbol=BTCUSDT" | openssl dgst -sha256 -hmac "secret"
result = hmac_sha256("secret", "symbol=BTCUSDT")
assert isinstance(result, str)
assert len(result) == 64 # hex-encoded SHA-256 is always 64 chars
def test_hmac_sha256_deterministic():
a = hmac_sha256("key", "msg")
b = hmac_sha256("key", "msg")
assert a == b
def test_hmac_sha256_different_secrets():
assert hmac_sha256("key1", "msg") != hmac_sha256("key2", "msg")
def test_sha256_string():
result = sha256("hello")
assert result == "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
def test_sha256_bytes():
result = sha256(b"hello")
assert result == "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
def test_md5_string():
result = md5("hello")
assert result == "5d41402abc4b2a76b9719d911017c592"
def test_generate_nonce_length():
nonce = generate_nonce(32)
assert len(nonce) == 32
nonce64 = generate_nonce(64)
assert len(nonce64) == 64
def test_generate_nonce_is_unique():
assert generate_nonce() != generate_nonce()
def test_timestamp_nonce_is_numeric_string():
ts = timestamp_nonce()
assert ts.isdigit()
assert len(ts) == 13 # milliseconds since epoch