Пример #1
0
    def decode(buffer: bytes, **kwargs) -> object:
        """
        Decodes a JWK JSON string into EdDSA parameters.

        Parameters:
            buffer (bytes/str): JWK JSON string.
        
        Returns:
            EdDSA: EdDSA object.
        """
        from samson.public_key.eddsa import EdDSA
        from samson.protocols.dh25519 import DH25519

        if issubclass(type(buffer), (bytes, bytearray)):
            buffer = buffer.decode()

        jwk = json.loads(buffer)

        curve = JWK_INVERSE_CURVE_LOOKUP[jwk['crv']]
        x = Bytes(url_b64_decode(jwk['x'].encode('utf-8')), 'little')

        if 'd' in jwk:
            d = Bytes(url_b64_decode(jwk['d'].encode('utf-8'))).int()
        else:
            d = 0

        if jwk['crv'] in ['Ed25519', 'Ed448']:
            eddsa = EdDSA(curve=curve, d=d)
            eddsa.A = eddsa.decode_point(x)
        else:
            eddsa = DH25519(curve=curve, d=d, pub=x.int())

        return eddsa
Пример #2
0
    def decode(buffer: bytes, **kwargs):
        from samson.public_key.eddsa import EdDSA
        items = bytes_to_der_sequence(buffer)

        pub_point = Bytes(int(items[1]))

        curve_oid = str(items[0][0])
        curve = EDCURVE_OID_LOOKUP[curve_oid]

        eddsa = EdDSA(curve=curve)
        eddsa.A = eddsa.decode_point(pub_point)

        return eddsa