def encode(ecdsa_key: object, **kwargs): alg_id = SequenceOf() alg_id.setComponentByPosition( 0, ObjectIdentifier([1, 2, 840, 10045, 2, 1])) alg_id.setComponentByPosition( 1, ObjectIdentifier( ber_decoder.decode(b'\x06' + bytes([len(ecdsa_key.G.curve.oid)]) + ecdsa_key.G.curve.oid)[0].asTuple())) zero_fill = math.ceil(ecdsa_key.G.curve.q.bit_length() / 8) params_seq = Sequence() params_seq.setComponentByPosition(0, Integer(1)) params_seq.setComponentByPosition( 1, OctetString(Bytes(ecdsa_key.d).zfill(zero_fill))) params_seq.setComponentByPosition( 2, PublicPoint(ecdsa_key.format_public_point())) param_oct = OctetString(encoder.encode(params_seq)) top_seq = Sequence() top_seq.setComponentByPosition(0, Integer(0)) top_seq.setComponentByPosition(1, alg_id) top_seq.setComponentByPosition(2, param_oct) encoded = encoder.encode(top_seq) encoded = PKCS8ECDSAPrivateKey.transport_encode(encoded, **kwargs) return encoded
def encode(eddsa_key: object, **kwargs): alg_id = SequenceOf() alg_id.setComponentByPosition(0, ObjectIdentifier(eddsa_key.curve.oid)) seq = Sequence() seq.setComponentByPosition(0, alg_id) seq.setComponentByPosition(1, X509EdDSASubjectPublicKey.encode(eddsa_key)) encoded = encoder.encode(seq) return X509EdDSAPublicKey.transport_encode(encoded, **kwargs)
def encode(eddsa_key: object, **kwargs): alg_id = SequenceOf() alg_id.setComponentByPosition(0, ObjectIdentifier(eddsa_key.curve.oid)) zero_fill = math.ceil(eddsa_key.d.int().bit_length() / 8) priv_key = OctetString( encoder.encode( OctetString(Bytes.wrap(eddsa_key.d).zfill(zero_fill)))) top_seq = Sequence() top_seq.setComponentByPosition(0, Integer(0)) top_seq.setComponentByPosition(1, alg_id) top_seq.setComponentByPosition(2, priv_key) encoded = encoder.encode(top_seq) encoded = PKCS8EdDSAPrivateKey.transport_encode(encoded, **kwargs) return encoded
def encode(dh_key: object, **kwargs): dh_params = SequenceOf() dh_params.setComponentByPosition(0, Integer(dh_key.p)) dh_params.setComponentByPosition(1, Integer(dh_key.g)) alg_id = Sequence() alg_id.setComponentByPosition(0, ObjectIdentifier([1, 2, 840, 113549, 1, 3, 1])) alg_id.setComponentByPosition(1, dh_params) param_oct = OctetString(encoder.encode(Integer(dh_key.key))) top_seq = Sequence() top_seq.setComponentByPosition(0, Integer(0)) top_seq.setComponentByPosition(1, alg_id) top_seq.setComponentByPosition(2, param_oct) encoded = encoder.encode(top_seq) encoded = PKCS8DiffieHellmanPrivateKey.transport_encode(encoded, **kwargs) return encoded
def encode(dsa_key: object, **kwargs): dss_params = SequenceOf() dss_params.setComponentByPosition(0, Integer(dsa_key.p)) dss_params.setComponentByPosition(1, Integer(dsa_key.q)) dss_params.setComponentByPosition(2, Integer(dsa_key.g)) alg_id = Sequence() alg_id.setComponentByPosition( 0, ObjectIdentifier([1, 2, 840, 10040, 4, 1])) alg_id.setComponentByPosition(1, dss_params) param_oct = OctetString(encoder.encode(Integer(dsa_key.x))) top_seq = Sequence() top_seq.setComponentByPosition(0, Integer(0)) top_seq.setComponentByPosition(1, alg_id) top_seq.setComponentByPosition(2, param_oct) encoded = encoder.encode(top_seq) encoded = PKCS8DSAPrivateKey.transport_encode(encoded, **kwargs) return encoded