mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-10 11:59:23 +00:00
various deps updates
This commit is contained in:
0
eboutic/tests/__init__.py
Normal file
0
eboutic/tests/__init__.py
Normal file
@ -1,39 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
#
|
||||
# Skia < skia AT libskia DOT so >
|
||||
#
|
||||
# Beerware licensed software - 2017
|
||||
#
|
||||
|
||||
import base64
|
||||
|
||||
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,
|
||||
)
|
||||
|
||||
with open("./private_key.pem", "br") as f:
|
||||
PRIVKEY = f.read()
|
||||
with open("./public_key.pem", "br") as f:
|
||||
PUBKEY = f.read()
|
||||
|
||||
data = "Amount=400&BasketID=4000&Auto=42&Error=00000\n".encode("utf-8")
|
||||
|
||||
# Sign
|
||||
privkey: RSAPrivateKey = load_pem_private_key(PRIVKEY, None)
|
||||
signature = privkey.sign(data, PKCS1v15(), SHA1())
|
||||
b64sig = base64.b64encode(signature)
|
||||
print(b64sig)
|
||||
|
||||
# Verify
|
||||
pubkey = load_pem_public_key(PUBKEY)
|
||||
signature = base64.b64decode(b64sig)
|
||||
try:
|
||||
pubkey.verify(signature, data, PKCS1v15(), SHA1())
|
||||
print("Verify OK")
|
||||
except InvalidSignature as e:
|
||||
print("Verify failed")
|
41
eboutic/tests/test_crypto.py
Executable file
41
eboutic/tests/test_crypto.py
Executable file
@ -0,0 +1,41 @@
|
||||
#!/usr/bin/env python3
|
||||
#
|
||||
# Skia < skia AT libskia DOT so >
|
||||
#
|
||||
# Beerware licensed software - 2017
|
||||
#
|
||||
|
||||
import base64
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from cryptography.exceptions import InvalidSignature
|
||||
from cryptography.hazmat.primitives.asymmetric.padding import PKCS1v15
|
||||
from cryptography.hazmat.primitives.asymmetric.rsa import RSAPrivateKey, RSAPublicKey
|
||||
from cryptography.hazmat.primitives.hashes import SHA1
|
||||
from cryptography.hazmat.primitives.serialization import (
|
||||
load_pem_private_key,
|
||||
load_pem_public_key,
|
||||
)
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
def test_signature_valid():
|
||||
"""Test that data sent to the bank is correctly signed."""
|
||||
data = "Amount=400&BasketID=4000&Auto=42&Error=00000\n".encode("utf-8")
|
||||
|
||||
# Sign
|
||||
key_dir = Path(settings.BASE_DIR) / "eboutic" / "tests"
|
||||
privkey: RSAPrivateKey = load_pem_private_key(
|
||||
(key_dir / "private_key.pem").read_bytes(), None
|
||||
)
|
||||
pubkey: RSAPublicKey = load_pem_public_key(
|
||||
(key_dir / "public_key.pem").read_bytes()
|
||||
)
|
||||
signature = privkey.sign(data, PKCS1v15(), SHA1())
|
||||
b64sig = base64.b64encode(signature)
|
||||
signature = base64.b64decode(b64sig)
|
||||
try:
|
||||
pubkey.verify(signature, data, PKCS1v15(), SHA1())
|
||||
except InvalidSignature:
|
||||
pytest.fail("Failed to validate signature")
|
Reference in New Issue
Block a user