def key_info(self, pkey, ref_uri, x509, cer_string, mime_type, key_info_template): exponent = base64.b64encode(pkey.e[4:]) modulus = m2.bn_to_hex(m2.mpi_to_bn( pkey.n)).decode("hex").encode("base64") return key_info_template % { 'modulus': modulus, 'exponent': exponent, 'certificate': cer_string, 'certificate_digest_value': self.sha1_hash_digest(cer_string), 'signingTime': datetime.datetime.now(timezone('America/Guayaquil')).isoformat(), 'ref_uri': ref_uri, 'mime_type': mime_type, 'issuer_name': self.get_issuer(x509), 'serial_number': x509.get_serial_number(), }
def rsa_key_info(pkey): "Convert private key (PEM) to XML Signature format (RSAKeyValue)" exponent = base64.b64encode(pkey.e[4:]) modulus = m2.bn_to_hex(m2.mpi_to_bn(pkey.n)).decode("hex").encode("base64") return KEY_INFO_TMPL % { 'modulus': modulus, 'exponent': exponent, }
def key_info(pkey, cert, key_info_template): "Convert private key (PEM) to XML Signature format (RSAKeyValue/X509Data)" exponent = base64.b64encode(pkey.e[4:]) modulus = m2.bn_to_hex(m2.mpi_to_bn(pkey.n)).decode("hex").encode("base64") x509 = x509_parse_cert(cert) if cert else None return key_info_template % { 'modulus': modulus, 'exponent': exponent, 'issuer_name': x509.get_issuer().as_text() if x509 else "", 'serial_number': x509.get_serial_number() if x509 else "", }
def mpi_to_num(mpi): return int(m2.bn_to_hex(m2.mpi_to_bn(mpi)), 16)
data = ba.unhexlify('01') data = hashlib.sha256(data).digest() print "sha256 is ",ba.hexlify(data),len(data) data = b''+data + b'\x00'*(256-len(data)) #padding with 0 print len(data) print ba.hexlify(data) cipher = rsa.private_encrypt(data, RSA.no_padding) s = ba.hexlify(cipher) print ba.hexlify(cipher),"\n" data2 = rsa.public_decrypt(cipher, RSA.no_padding) data = ba.unhexlify('01') data = hashlib.sha256(data).digest() print dir(rsa) print ba.hexlify(rsa.sign(data, 'sha256')) print "N:", m2.bn_to_hex(m2.mpi_to_bn(rsa.n)) print "E:", m2.bn_to_hex(m2.mpi_to_bn(rsa.e)) print "D:", m2.bn_to_hex(m2.mpi_to_bn(rsa.d)) exit() # =================================================================== ''' print "public encrypt pkcs1_padding\n" cipher = rsa.public_encrypt(data, RSA.pkcs1_padding) cipher2 = rsa.public_encrypt(data, RSA.pkcs1_padding) print ba.hexlify(cipher),"\n" print ba.hexlify(cipher2),"\n" data2 = rsa.private_decrypt(cipher, RSA.pkcs1_padding) ''' print(dir(RSA)) exit()