def Read(key): """ Reads a RSA public key from a JSON string representation of it. @param key: a JSON representation of a RSA public key @type key: string @return: a RSA public key @rtype: L{RsaPublicKey} """ rsa = json.loads(key) params = {'modulus' : util.Decode(rsa['modulus']), 'publicExponent' : util.Decode(rsa['publicExponent'])} pubkey = RSA.construct((util.BytesToLong(params['modulus']), util.BytesToLong(params['publicExponent']))) return RsaPublicKey(params, pubkey, rsa['size'])
def Read(key): """ Reads a DSA private key from a JSON string representation of it. @param key: a JSON representation of a DSA private key @type key: string @return: an DSA private key @rtype: L{DsaPrivateKey} """ dsa = json.loads(key) pub = DsaPublicKey.Read(json.dumps(dsa['publicKey'])) params = {'x': util.Base64WSDecode(dsa['x'])} key = DSA.construct((util.BytesToLong(pub._params['y']), util.BytesToLong(pub._params['g']), util.BytesToLong(pub._params['p']), util.BytesToLong(pub._params['q']), util.BytesToLong(params['x']))) return DsaPrivateKey(params, pub, key, dsa['size'])
def Read(key): """ Reads a DSA public key from a JSON string representation of it. @param key: a JSON representation of a DSA public key @type key: string @return: a DSA public key @rtype: L{DsaPublicKey} """ dsa = json.loads(key) params = {'y' : util.Decode(dsa['y']), 'p' : util.Decode(dsa['p']), 'g' : util.Decode(dsa['g']), 'q' : util.Decode(dsa['q'])} pubkey = DSA.construct((util.BytesToLong(params['y']), util.BytesToLong(params['g']), util.BytesToLong(params['p']), util.BytesToLong(params['q']))) return DsaPublicKey(params, pubkey, dsa['size'])
def Read(key): """ Reads a RSA private key from a JSON string representation of it. @param key: a JSON representation of a RSA private key @type key: string @return: a RSA private key @rtype: L{RsaPrivateKey} """ rsa = json.loads(key) pub = RsaPublicKey.Read(json.dumps(rsa['publicKey'])) params = { 'privateExponent': util.Base64WSDecode(rsa['privateExponent']), 'primeP': util.Base64WSDecode(rsa['primeP']), 'primeQ': util.Base64WSDecode(rsa['primeQ']), 'primeExponentP': util.Base64WSDecode(rsa['primeExponentP']), 'primeExponentQ': util.Base64WSDecode(rsa['primeExponentQ']), 'crtCoefficient': util.Base64WSDecode(rsa['crtCoefficient']) } key = RSA.construct((util.BytesToLong(pub.params['modulus']), util.BytesToLong(pub.params['publicExponent']), util.BytesToLong(params['privateExponent']), util.BytesToLong(params['primeQ']), util.BytesToLong(params['primeP']), util.BytesToLong(params['crtCoefficient']))) return RsaPrivateKey(params, pub, key, rsa['size'])
def Verify(self, msg, sig): """ Return True if the signature corresponds to the message. @param msg: message that has been signed @type msg: string @param sig: string representation of long int signature @type sig: string @return: True if signature is valid for the message hash. False otherwise. @rtype: boolean """ try: return self.key.verify(util.MakeEmsaMessage(msg, self.size), (util.BytesToLong(sig),)) except ValueError: # if sig is not a long, it's invalid return False