Sith/eboutic/tests/test.py

35 lines
760 B
Python
Raw Permalink Normal View History

2017-05-01 17:39:13 +00:00
#!/usr/bin/env python3
# -*- coding:utf-8 -*
#
# Skia < skia AT libskia DOT so >
#
# Beerware licensed software - 2017
#
import base64
from OpenSSL import crypto
with open("./private_key.pem") as f:
PRVKEY = f.read()
with open("./public_key.pem") as f:
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
prvkey = crypto.load_privatekey(crypto.FILETYPE_PEM, PRVKEY)
2022-08-03 19:48:37 +00:00
sig = crypto.sign(prvkey, data, "sha1")
2017-05-01 17:39:13 +00:00
b64sig = base64.b64encode(sig)
print(b64sig)
# Verify
pubkey = crypto.load_publickey(crypto.FILETYPE_PEM, PUBKEY)
cert = crypto.X509()
cert.set_pubkey(pubkey)
sig = base64.b64decode(b64sig)
try:
2022-08-03 19:48:37 +00:00
crypto.verify(cert, sig, data, "sha1")
2017-05-01 17:39:13 +00:00
print("Verify OK")
except:
print("Verify failed")