def decode(buffer: bytes, **kwargs) -> object: """ Decodes a JWK JSON string into an RSA object. Parameters: buffer (bytes/str): JWK JSON string. Returns: RSA: RSA object. """ from samson.public_key.rsa import RSA if issubclass(type(buffer), (bytes, bytearray)): buffer = buffer.decode() jwk = json.loads(buffer) n = Bytes(url_b64_decode(jwk['n'].encode('utf-8'))).int() e = Bytes(url_b64_decode(jwk['e'].encode('utf-8'))).int() if 'p' in jwk: p = Bytes(url_b64_decode(jwk['p'].encode('utf-8'))).int() q = Bytes(url_b64_decode(jwk['q'].encode('utf-8'))).int() else: p = 2 q = 3 rsa = RSA(8, p=p, q=q, e=e) rsa.n = n rsa.bits = rsa.n.bit_length() return rsa
def decode(buffer: bytes, **kwargs): from samson.public_key.rsa import RSA _, pub = parse_openssh_key(buffer, SSH_PUBLIC_HEADER, RSAPublicKey, RSAPrivateKey, None) n, e, p, q = pub.n, pub.e, 2, 3 rsa = RSA(8, p=p, q=q, e=e) rsa.n = n return rsa
def decode(buffer: bytes, **kwargs): from samson.public_key.rsa import RSA priv, _ = parse_openssh_key(buffer, SSH_PUBLIC_HEADER, RSAPublicKey, RSAPrivateKey, kwargs.get('passphrase')) n, e, p, q = priv.n, priv.e, priv.p, priv.q rsa = RSA(8, p=p, q=q, e=e) rsa.n = n return rsa
def decode(buffer: bytes, **kwargs): from samson.public_key.rsa import RSA items = bytes_to_der_sequence(buffer) items = [int(item) for item in items] n, e = items rsa = RSA(8, e=e) rsa.n = n rsa.bits = rsa.n.bit_length() return rsa
def _run_oaep(self, e, d, modulus, bits, message, seed, expected_ciphertext): rsa = RSA(bits) rsa.e = e rsa.d = d rsa.n = modulus oaep = OAEP(rsa.bits) padded_plain = oaep.pad(message, seed=seed) ciphertext = Bytes(rsa.encrypt(padded_plain)) self.assertEqual(ciphertext, expected_ciphertext) self.assertEqual(oaep.unpad(rsa.decrypt(ciphertext.int())), message)
def decode(buffer: bytes, **kwargs): from samson.public_key.rsa import RSA items = bytes_to_der_sequence(buffer) if type(items[1]) is BitString: if str(items[0][0]) == '1.2.840.113549.1.1.1': bitstring_seq = decoder.decode(Bytes(int(items[1])))[0] items = list(bitstring_seq) else: raise ValueError('Unable to decode RSA key.') n, e = [int(item) for item in items] rsa = RSA(8, e=e) rsa.n = n rsa.bits = rsa.n.bit_length() return rsa