1
0
mirror of https://github.com/Mailu/Mailu.git synced 2024-12-14 10:53:30 +02:00
Mailu/core/admin/mailu/dkim.py

22 lines
876 B
Python
Raw Normal View History

2016-06-25 15:50:05 +02:00
""" No crypto operation is done on keys.
They are thus represented as ASCII armored PEM.
"""
2022-11-27 16:41:21 +02:00
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
2016-06-25 15:50:05 +02:00
2022-11-27 16:41:21 +02:00
def gen_key(bits=2048):
2016-06-25 15:50:05 +02:00
""" Generate and return a new RSA key.
"""
2022-11-27 16:41:21 +02:00
k = rsa.generate_private_key(public_exponent=65537, key_size=bits)
return k.private_bytes(encoding=serialization.Encoding.PEM,format=serialization.PrivateFormat.PKCS8,encryption_algorithm=serialization.NoEncryption())
2016-06-25 15:50:05 +02:00
def strip_key(pem):
""" Return only the b64 part of the ASCII armored PEM.
"""
2022-11-27 16:41:21 +02:00
priv_key = serialization.load_pem_private_key(pem, password=None)
public_pem = priv_key.public_key().public_bytes(encoding=serialization.Encoding.PEM,format=serialization.PublicFormat.SubjectPublicKeyInfo)
2016-06-25 15:50:05 +02:00
return public_pem.replace(b"\n", b"").split(b"-----")[2]