def test_encrypt():
    assert Cryptographer.encrypt(data, key) == encrypted_data
def test_decrypt():
    # Valid data should run with no InvalidTag and verify
    assert Cryptographer.decrypt(encrypted_data, key) == data
def test_encrypt_odd_length_data():
    blob = get_random_value_hex(64)[-1]
    key = get_random_value_hex(32)

    with pytest.raises(InvalidParameter, match="Odd-length"):
        Cryptographer.encrypt(blob, key)
def test_encrypt_wrong_key_size():
    blob = get_random_value_hex(64)
    key = get_random_value_hex(31)

    with pytest.raises(InvalidParameter, match="32-byte hex value"):
        Cryptographer.encrypt(blob, key)
def test_check_data_key_format_wrong_key():
    data = get_random_value_hex(64)
    key = get_random_value_hex(33)

    with pytest.raises(InvalidParameter, match="32-byte hex value"):
        Cryptographer.check_data_key_format(data, key)
def test_check_data_key_format():
    data = get_random_value_hex(64)
    key = get_random_value_hex(32)

    # Correct format does not raise anything
    Cryptographer.check_data_key_format(data, key)
def test_get_compressed_pk_wrong_type():
    # Passing a value that is not a PublicKey will make it to fail too
    pk = get_random_value_hex(33)

    with pytest.raises(InvalidParameter, match="Wrong value passed as pk"):
        Cryptographer.get_compressed_pk(pk)
def test_get_compressed_pk_wrong_key():
    # pk should be properly initialized. Initializing from int will cause it to not be recoverable
    pk = PublicKey(0)

    with pytest.raises(InvalidKey, match="PublicKey has invalid initializer"):
        Cryptographer.get_compressed_pk(pk)
def test_get_compressed_pk():
    sk, pk = generate_keypair()
    compressed_pk = Cryptographer.get_compressed_pk(pk)

    assert isinstance(compressed_pk, str) and len(compressed_pk) == 66
    assert compressed_pk[:2] in ["02", "03"]
def test_check_data_key_format_wrong_data():
    data = get_random_value_hex(64)[:-1]
    key = get_random_value_hex(32)

    with pytest.raises(InvalidParameter, match="Odd-length"):
        Cryptographer.check_data_key_format(data, key)
def test_sign_wrong_sk():
    # If a sk is not passed, sign will return None
    for wtype in WRONG_TYPES:
        with pytest.raises(InvalidParameter, match="Wrong value passed as sk"):
            Cryptographer.sign(b"", wtype)