def public_key_from_private( private_key: Ed25519PrivateKey) -> Ed25519PublicKey: """ Returns the corresponding Ed25519PublicKey for a Ed25519PrivateKey Args: private_key (Ed25519PrivateKey): Private key. Returns: Ed25519PublicKey: Public key. """ return private_key.public_key()
def __init__(self, private_key: Ed25519PrivateKey, domain: str = CORD_DOMAIN): self.private_key: Ed25519PrivateKey = private_key self.public_key: Ed25519PublicKey = private_key.public_key() self._public_key_hex: str = self.public_key.public_bytes( Encoding.Raw, PublicFormat.Raw).hex() self.domain = domain endpoint = domain + CORD_PUBLIC_USER_PATH super().__init__(endpoint)
def encode_private(self, private_key: ed25519.Ed25519PrivateKey, f_priv: _FragList) -> None: """Write Ed25519 private key""" public_key = private_key.public_key() raw_private_key = private_key.private_bytes(Encoding.Raw, PrivateFormat.Raw, NoEncryption()) raw_public_key = public_key.public_bytes(Encoding.Raw, PublicFormat.Raw) f_keypair = _FragList([raw_private_key, raw_public_key]) self.encode_public(public_key, f_priv) f_priv.put_sshstr(f_keypair)
def _get_key_pair_from_sk( sk: ed25519.Ed25519PrivateKey) -> typing.Tuple[bytes, bytes]: """Returns key pair from a signing key. """ pk = sk.public_key() return \ sk.private_bytes( encoding=serialization.Encoding.Raw, format=serialization.PrivateFormat.Raw, encryption_algorithm=serialization.NoEncryption() ), \ pk.public_bytes( encoding=serialization.Encoding.Raw, format=serialization.PublicFormat.Raw )
def _get_key_pair(pvk: ed25519.Ed25519PrivateKey, encoding: KeyEncoding) -> typing.Tuple[str, str]: """Maps private key to an encoded key pair. """ pbk = pvk.public_key() # PEM. if encoding == KeyEncoding.PEM: return \ pvk.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption() ), \ pbk.public_bytes( encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo ) # Encode -> bytes. pvk = pvk.private_bytes( encoding=serialization.Encoding.Raw, format=serialization.PrivateFormat.Raw, encryption_algorithm=serialization.NoEncryption() ) pbk = pbk.public_bytes( encoding=serialization.Encoding.Raw, format=serialization.PublicFormat.Raw ) # HEX. if encoding == KeyEncoding.HEX: return pvk.hex(), pbk.hex() # BYTES. return pvk, pbk