File size: 896 Bytes
5b01a63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.serialization import (
    load_pem_private_key, load_pem_public_key
)
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.exceptions import InvalidSignature
from authlib.common.encoding import to_bytes


def sign_sha1(msg, rsa_private_key):
    key = load_pem_private_key(
        to_bytes(rsa_private_key),
        password=None,
        backend=default_backend()
    )
    return key.sign(msg, padding.PKCS1v15(), hashes.SHA1())


def verify_sha1(sig, msg, rsa_public_key):
    key = load_pem_public_key(
        to_bytes(rsa_public_key),
        backend=default_backend()
    )
    try:
        key.verify(sig, msg, padding.PKCS1v15(), hashes.SHA1())
        return True
    except InvalidSignature:
        return False