def rep(self, passphrase=None, algorithm=crypto.SYMALGORITHM_AES256): passphrase = self.passphrase keydata = self.d.rep() + self.p.rep() + self.q.rep() + self.u.rep() s2kPart = '\x00' if passphrase is not None and len(passphrase) > 0: iv = crypto.randomBytes(crypto.SYMALGORITHM_BLOCKSIZE[algorithm]) s2k = S2KElement() s2kPart = '\xfe' + chr(algorithm) + s2k.rep() + iv keydata += crypto.hash_sha1(keydata) keydata = crypto.encryptCFB(s2k.generateKey(passphrase, algorithm), keydata, iv, algorithm) else: keydata += '\x00\x00' data = (self.version.rep(1) + self.created.rep() + self.algorithm.rep(1) + self.n.rep() + self.e.rep() + s2kPart + keydata) return Packet.createHeader(self.TAG, len(data)) + data
def testHash(self): self.assertEqual(crypto.hash_sha1('abc').encode('hex'), 'a9993e364706816aba3e25717850c26c9cd0d89d') self.assertEqual(crypto.hash_sha256('abc').encode('hex'), 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410f' 'f61f20015ad') self.assertEqual(crypto.hash_sha384('abc').encode('hex'), 'cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b6' '05a43ff5bed8086072ba1e7cc2358baeca134c825a7') self.assertEqual(crypto.hash_sha512('abc').encode('hex'), 'ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9ee' 'ee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d442364' '3ce80e2a9ac94fa54ca49f')
def __init__(self, s=None, length=0, passphraseCallback=None): PublicKeyPacket.__init__(self, s) self.passphrase = None if s is None: return self.s2kUsage = ord(s.read(1)) if self.s2kUsage == 255 or self.s2kUsage == 254: self.symAlgorithm = ord(s.read(1)) self.s2k = S2KElement(s) self.iv = s.read(crypto.SYMALGORITHM_BLOCKSIZE[self.symAlgorithm]) encrypted = s.read(length - s.tell() + 3) if passphraseCallback is None: raise Exception('encrypted key and no passphraseCallback') symkey = self.s2k.generateKey(passphraseCallback(), self.symAlgorithm) decrypted = io.BytesIO(crypto.decryptCFB(symkey, encrypted, self.iv, self.symAlgorithm)) self.d = MPIElement(decrypted) self.p = MPIElement(decrypted) self.q = MPIElement(decrypted) self.u = MPIElement(decrypted) if self.s2kUsage == 254: self.checksum = decrypted.read(20) if self.checksum != crypto.hash_sha1(decrypted.getvalue()[:-20]): raise Exception('invalid passphrase') else: raise Exception('not implemented') elif self.s2kUsage == 0: self.d = MPIElement(s) self.p = MPIElement(s) self.q = MPIElement(s) self.u = MPIElement(s) self.checksum = s.read(2) else: raise Exception('not implemented')
def fingerprint(self): return crypto.hash_sha1(self.hashdata())