Last active
October 17, 2021 13:56
-
-
Save Junzki/44c91f085b133b6cb079e08aaed703ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
""" | |
BSD 3-Clause License | |
Copyright (c) 2021, Andrew Junzki | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions are met: | |
1. Redistributions of source code must retain the above copyright notice, this | |
list of conditions and the following disclaimer. | |
2. Redistributions in binary form must reproduce the above copyright notice, | |
this list of conditions and the following disclaimer in the documentation | |
and/or other materials provided with the distribution. | |
3. Neither the name of the copyright holder nor the names of its | |
contributors may be used to endorse or promote products derived from | |
this software without specific prior written permission. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
""" | |
import base64 | |
import dataclasses | |
import hashlib | |
from cryptography.hazmat.primitives import serialization | |
from cryptography.hazmat.primitives.asymmetric import rsa | |
from cryptography.hazmat.primitives.asymmetric import ed25519 | |
from cryptography.hazmat.backends import default_backend | |
@dataclasses.dataclass | |
class Keypair: | |
public: bytes | |
private: bytes | |
comment: bytes = b'' | |
@property | |
def fingerprint(self, alg: str = 'sha256') -> bytes: | |
_key = self.public.split(b' ')[1] # Syntax: `ssh-[alg] [key] [comment]` | |
payload = base64.b64decode(_key) | |
plain = hashlib.sha256(payload) | |
return base64.b64encode(plain).strip(b'=') # Base64 with no padding. | |
class _Generator(object): | |
def generate(self, comment: str = ''): | |
raise NotImplementedError("generate method should be implmented.") | |
@staticmethod | |
def append_comment(pub: bytes, comment: str = '') -> (bytes, bytes): | |
if not comment: | |
return pub, b'' | |
comment = comment.encode() | |
return b''.join(pub, comment), comment | |
class RsaGenerator(_Generator): | |
ALGORITHM = 'rsa' | |
def __init__(self, size: int, *args, **kwargs): | |
self.size = size | |
def generate(self, comment: str = '') -> Keypair: | |
_gen = rsa.generate_private_key( | |
public_exponent=65537, | |
key_size=size, | |
backend=default_backend() | |
) | |
private = _gen.private_bytes( | |
serialization.Encoding.PEM, | |
serialization.PrivateFormat.PKCS8, # OpenSSH uses PKCS #8 formatted private key. | |
serialization.NoEncryption() # No encryption required if no passphase set. | |
) | |
pub = _gen.public_key().public_bytes( | |
serialization.Encoding.OpenSSH, | |
serialization.PublicFormat.OpenSSH | |
) | |
pub, comment = self.append_comment(pub, comment) | |
return Keypair(pub, private, comment) | |
class Ed25519Generator(_Generator): | |
ALGORITHM = 'ed25519' | |
def __init__(self, *args, **kwargs): | |
self._gen = ed25519.Ed25519PrivateKey | |
def generate(self, comment: str) -> Keypair: | |
_key = self._gen.generate() | |
pub = _key.public_key() | |
private = _key.private_bytes( | |
serialization.Encoding.PEM, | |
serialization.PrivateFormat.PKCS8, | |
serialization.NoEncryption() | |
) | |
pub = _key.public_key().public_bytes( | |
serialization.Encoding.OpenSSH, | |
serialization.PublicFormat.OpenSSH | |
) | |
pub, comment = self.append_comment(pub, comment) | |
return Keypair(pub, private, comment) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment