Server working
This commit is contained in:
@@ -19,11 +19,13 @@ class PrivateKey:
|
||||
@classmethod
|
||||
def load(cls, pem):
|
||||
"""Loads an existing private key.
|
||||
When `pem` is of type bytes, assumes it is a pem serialization.
|
||||
Otherwise, assumes `pem` is a path to a pem file.
|
||||
Pem may be a bytes or a string representation of the PEM-formatted key,
|
||||
or a path to a pem file.
|
||||
"""
|
||||
if isinstance(pem, bytes):
|
||||
key = serialization.load_pem_private_key(pem)
|
||||
key = serialization.load_pem_private_key(pem, password=None)
|
||||
elif isinstance(pem, str) and pem.startswith("-----BEGIN RSA PRIVATE KEY-----"):
|
||||
key = serialization.load_pem_private_key(pem.encode('ascii'), password=None)
|
||||
elif isinstance(pem, (str, Path)):
|
||||
with open(pem, 'rb') as key_file:
|
||||
key = serialization.load_pem_private_key(
|
||||
@@ -58,7 +60,7 @@ class PrivateKey:
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PrivateFormat.TraditionalOpenSSL,
|
||||
encryption_algorithm=serialization.NoEncryption()
|
||||
).decode('utf8')
|
||||
).decode('utf8').strip()
|
||||
|
||||
def __repr__(self):
|
||||
return "<PrivateKey>"
|
||||
@@ -106,11 +108,13 @@ class PublicKey:
|
||||
@classmethod
|
||||
def load(self, pem):
|
||||
"""Loads an existing public key.
|
||||
When `pem` is of type bytes, assumes it is a pem serialization.
|
||||
Otherwise, assumes `pem` is a path to a pem file.
|
||||
Pem may be a bytes or a string representation of the PEM-formatted key,
|
||||
or a path to a pem file.
|
||||
"""
|
||||
if isinstance(pem, bytes):
|
||||
key = serialization.load_pem_public_key(pem)
|
||||
elif isinstance(pem, str) and pem.startswith("-----BEGIN PUBLIC KEY-----"):
|
||||
key = serialization.load_pem_public_key(pem.encode('ascii'))
|
||||
elif isinstance(pem, (str, Path)):
|
||||
with open(pem, 'rb') as key_file:
|
||||
key = serialization.load_pem_public_key(key_file.read())
|
||||
@@ -126,7 +130,7 @@ class PublicKey:
|
||||
return self.key.public_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
||||
).decode('utf8')
|
||||
).decode('utf8').strip()
|
||||
|
||||
def __repr__(self):
|
||||
return "<PublicKey>"
|
||||
|
||||
Reference in New Issue
Block a user