Sith/eboutic/tests/test.py

40 lines
1.1 KiB
Python
Raw Normal View History

2017-05-01 17:39:13 +00:00
#!/usr/bin/env python3
#
# Skia < skia AT libskia DOT so >
#
# Beerware licensed software - 2017
#
import base64
2024-06-24 11:07:36 +00:00
2024-06-26 13:29:05 +00:00
from cryptography.exceptions import InvalidSignature
from cryptography.hazmat.primitives.asymmetric.padding import PKCS1v15
from cryptography.hazmat.primitives.asymmetric.rsa import RSAPrivateKey
from cryptography.hazmat.primitives.hashes import SHA1
from cryptography.hazmat.primitives.serialization import (
load_pem_private_key,
load_pem_public_key,
)
2017-05-01 17:39:13 +00:00
2024-06-26 13:29:05 +00:00
with open("./private_key.pem", "br") as f:
PRIVKEY = f.read()
with open("./public_key.pem", "br") as f:
2017-05-01 17:39:13 +00:00
PUBKEY = f.read()
2022-08-03 19:48:37 +00:00
data = "Amount=400&BasketID=4000&Auto=42&Error=00000\n".encode("utf-8")
2017-05-01 17:39:13 +00:00
# Sign
2024-06-26 13:29:05 +00:00
privkey: RSAPrivateKey = load_pem_private_key(PRIVKEY, None)
signature = privkey.sign(data, PKCS1v15(), SHA1())
b64sig = base64.b64encode(signature)
2017-05-01 17:39:13 +00:00
print(b64sig)
# Verify
2024-06-26 13:29:05 +00:00
pubkey = load_pem_public_key(PUBKEY)
signature = base64.b64decode(b64sig)
2017-05-01 17:39:13 +00:00
try:
2024-06-26 13:29:05 +00:00
pubkey.verify(signature, data, PKCS1v15(), SHA1())
2017-05-01 17:39:13 +00:00
print("Verify OK")
2024-06-26 13:29:05 +00:00
except InvalidSignature as e:
2017-05-01 17:39:13 +00:00
print("Verify failed")