示例#1
0
def test_public_key_is_hashable():
    sk = SecretKey.random()
    pk = sk.public_key()

    sk2 = SecretKey.random()
    pk2 = sk2.public_key()
    assert hash(pk) != hash(pk2)

    pk3 = PublicKey.from_bytes(bytes(pk))
    assert hash(pk) == hash(pk3)
示例#2
0
def test_signature_str():
    sk = SecretKey.random()
    pk = sk.public_key()
    signer = Signer(sk)
    signature = signer.sign(b'peace at dawn')
    s = str(signature)
    assert 'Signature' in s
示例#3
0
def test_public_key_serialization():
    sk = SecretKey.random()
    pk = sk.public_key()

    encoded_key = bytes(pk)
    decoded_key = PublicKey.from_bytes(encoded_key)
    assert pk == decoded_key
示例#4
0
def test_gen_key():
    sk = SecretKey.random()
    assert type(sk) == SecretKey

    pk = sk.public_key()
    assert type(pk) == PublicKey

    pk2 = sk.public_key()
    assert pk == pk2
示例#5
0
def test_sign_and_verify(execution_number):
    sk = SecretKey.random()
    pk = sk.public_key()
    signer = Signer(sk)

    message = b"peace at dawn" + str(execution_number).encode()

    signature = signer.sign(message)
    assert signature.verify(pk, message)
示例#6
0
def test_sign_serialize_and_verify(execution_number):
    sk = SecretKey.random()
    pk = sk.public_key()
    signer = Signer(sk)

    message = b"peace at dawn" + str(execution_number).encode()

    signature = signer.sign(message)

    signature_bytes = bytes(signature)
    signature_restored = Signature.from_bytes(signature_bytes)

    assert signature_restored.verify(pk, message)
示例#7
0
def test_verification_fail():
    sk = SecretKey.random()
    pk = sk.public_key()
    signer = Signer(sk)

    message = b"peace at dawn"
    signature = signer.sign(message)

    # wrong message
    wrong_message = b"no peace at dawn"
    assert not signature.verify(pk, wrong_message)

    # bad signature
    signature_bytes = bytes(signature)
    signature_bytes = b'\x00' + signature_bytes[1:]
    signature_restored = Signature.from_bytes(signature_bytes)

    assert not signature_restored.verify(pk, message)
示例#8
0
def test_signature_is_hashable():
    sk = SecretKey.random()
    pk = sk.public_key()
    signer = Signer(sk)

    message = b'peace at dawn'
    message2 = b'no peace at dawn'

    signature = signer.sign(message)
    signature2 = signer.sign(message2)

    assert hash(signature) != hash(signature2)

    signature_restored = Signature.from_bytes(bytes(signature))
    assert signature == signature_restored
    assert hash(signature) == hash(signature_restored)

    # Different hash, since signing involves some randomness
    signature3 = signer.sign(message)
    assert hash(signature) != hash(signature3)
示例#9
0
def test_secret_scalar():
    sk = SecretKey.random()
    assert sk.secret_scalar() == sk._scalar_key
示例#10
0
def test_public_key_str():
    pk = SecretKey.random().public_key()
    s = str(pk)
    assert 'PublicKey' in s
示例#11
0
def test_public_key_point():
    pk = SecretKey.random().public_key()
    assert bytes(pk) == bytes(pk.point())
示例#12
0
def test_signer_pubkey():
    sk = SecretKey.random()
    pk = sk.public_key()
    signer = Signer(sk)
    assert signer.verifying_key() == pk
示例#13
0
def test_signer_hash():
    signer = Signer(SecretKey.random())
    # Insecure Python hash, shouldn't be available.
    with pytest.raises(RuntimeError):
        hash(signer)
示例#14
0
def test_signer_str():
    signer = Signer(SecretKey.random())
    s = str(signer)
    assert s == "Signer:..."
示例#15
0
def test_secret_key_serialization():
    sk = SecretKey.random()
    encoded_key = sk.to_secret_bytes()
    decoded_key = SecretKey.from_bytes(encoded_key)
    assert sk.to_secret_bytes() == decoded_key.to_secret_bytes()
示例#16
0
def test_secret_key_hash():
    sk = SecretKey.random()
    # Insecure Python hash, shouldn't be available.
    with pytest.raises(RuntimeError):
        hash(sk)
示例#17
0
def test_secret_key_str():
    sk = SecretKey.random()
    s = str(sk)
    assert s == "SecretKey:..."
示例#18
0
def test_signer_bytes():
    signer = Signer(SecretKey.random())
    # Shouldn't be able to serialize.
    with pytest.raises(RuntimeError):
        bytes(signer)