2019-10-05 09:34:57 +00:00
|
|
|
"""Cryptography primitives test module."""
|
|
|
|
|
2019-10-05 12:11:14 +00:00
|
|
|
import pytest
|
2019-10-05 18:28:55 +00:00
|
|
|
from merkle_tree_stream import MerkleTreeNode
|
2019-10-05 12:11:14 +00:00
|
|
|
from pysodium import crypto_sign_PUBLICKEYBYTES, crypto_sign_SECRETKEYBYTES
|
|
|
|
|
2019-11-14 12:08:54 +00:00
|
|
|
from hypercore_crypto import (
|
|
|
|
data,
|
|
|
|
discovery_key,
|
|
|
|
key_pair,
|
|
|
|
parent,
|
|
|
|
random_bytes,
|
|
|
|
sign,
|
|
|
|
verify,
|
|
|
|
)
|
2019-10-05 18:28:55 +00:00
|
|
|
|
2019-10-05 12:11:14 +00:00
|
|
|
|
|
|
|
def test_key_pair_seed_length():
|
|
|
|
with pytest.raises(ValueError) as exception:
|
2020-05-16 16:38:11 +00:00
|
|
|
key_pair(b"world hello")
|
|
|
|
assert "must be of length" in str(exception)
|
2019-10-05 09:34:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_key_pair_length():
|
|
|
|
public_key, secret_key = key_pair()
|
2019-10-05 12:11:14 +00:00
|
|
|
assert len(public_key) == crypto_sign_PUBLICKEYBYTES
|
|
|
|
assert len(secret_key) == crypto_sign_SECRETKEYBYTES
|
|
|
|
|
|
|
|
|
|
|
|
def test_sign():
|
2020-05-16 16:38:11 +00:00
|
|
|
message = b"hello world"
|
2019-10-05 12:11:14 +00:00
|
|
|
_, secret_key = key_pair()
|
2019-10-05 18:28:55 +00:00
|
|
|
signature = sign(message, secret_key)
|
|
|
|
assert message not in signature
|
|
|
|
assert len(signature) == 64
|
2019-10-05 12:11:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_verify():
|
2020-05-16 16:38:11 +00:00
|
|
|
message = b"hello world"
|
2019-10-05 12:11:14 +00:00
|
|
|
public_key, secret_key = key_pair()
|
|
|
|
signature = sign(message, secret_key)
|
|
|
|
assert verify(message, signature, public_key)
|
2019-10-05 18:28:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_data_digest():
|
|
|
|
assert (
|
2020-05-16 16:38:11 +00:00
|
|
|
data(b"hello world").hex()
|
|
|
|
== "ccfa4259ee7c41e411e5770973a49c5ceffb5272d6a37f2c6f2dac2190f7e2b7"
|
2019-10-05 18:28:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_random_bytes():
|
|
|
|
assert len(random_bytes(32)) == 32
|
|
|
|
|
|
|
|
|
|
|
|
def test_parent_digest():
|
2020-05-16 16:38:11 +00:00
|
|
|
_data = b"hello world"
|
2019-10-05 18:28:55 +00:00
|
|
|
_parent = parent(
|
|
|
|
MerkleTreeNode(
|
|
|
|
index=0, size=11, hash=data(_data), parent=None, data=None
|
|
|
|
),
|
|
|
|
MerkleTreeNode(
|
|
|
|
index=2, size=11, hash=data(_data), parent=None, data=None
|
|
|
|
),
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
_parent.hex()
|
2020-05-16 16:38:11 +00:00
|
|
|
== "43563406adba8b34b133fdca32d0a458c5be769615e01df30e6535ccd3c075f0"
|
2019-10-05 18:28:55 +00:00
|
|
|
)
|
2019-11-14 12:08:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_discovery_key_generated():
|
|
|
|
assert discovery_key(random_bytes(32)) is not None
|