2019-09-13 10:51:39 +00:00
|
|
|
import base64, typing
|
2019-09-18 09:18:49 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
from cryptography.hazmat.primitives import hashes, serialization
|
|
|
|
from cryptography.hazmat.primitives.asymmetric import padding, rsa
|
|
|
|
from cryptography.hazmat.backends import default_backend
|
|
|
|
has_crypto = True
|
|
|
|
except ModuleNotFoundError:
|
|
|
|
has_crypto = False
|
2019-09-13 10:51:39 +00:00
|
|
|
|
|
|
|
SIGNATURE_FORMAT = (
|
|
|
|
"keyId=\"%s\",headers=\"%s\",signature=\"%s\",algorithm=\"rsa-sha256\"")
|
|
|
|
|
2019-09-15 09:43:46 +00:00
|
|
|
def _private_key(key_filename: str) -> rsa.RSAPrivateKey:
|
2019-09-15 10:48:01 +00:00
|
|
|
with open(key_filename, "rb") as key_file:
|
2019-09-13 10:51:39 +00:00
|
|
|
return serialization.load_pem_private_key(
|
|
|
|
key_file.read(), password=None, backend=default_backend())
|
|
|
|
|
2019-09-15 09:43:46 +00:00
|
|
|
class PrivateKey(object):
|
|
|
|
def __init__(self, filename, id):
|
|
|
|
self.key = _private_key(filename)
|
|
|
|
self.id = id
|
|
|
|
|
|
|
|
def signature(key: PrivateKey, headers: typing.List[typing.Tuple[str, str]]
|
|
|
|
) -> str:
|
2019-09-13 10:51:39 +00:00
|
|
|
sign_header_keys = " ".join(h[0] for h in headers)
|
|
|
|
|
|
|
|
sign_string_parts = ["%s: %s" % (k, v) for k, v in headers]
|
|
|
|
sign_string = "\n".join(sign_string_parts)
|
|
|
|
|
2019-09-15 09:43:46 +00:00
|
|
|
signature = key.key.sign(
|
2019-09-13 10:51:39 +00:00
|
|
|
sign_string.encode("utf8"),
|
2019-09-16 09:52:57 +00:00
|
|
|
padding.PKCS1v15(),
|
2019-09-13 10:51:39 +00:00
|
|
|
hashes.SHA256()
|
|
|
|
)
|
|
|
|
|
|
|
|
signature = base64.b64encode(signature).decode("ascii")
|
2019-09-15 10:51:51 +00:00
|
|
|
return SIGNATURE_FORMAT % (key.id, sign_header_keys, signature)
|