def test_short(self): kd = pol.kd.KeyDerivation.setup({ 'bits': 256, 'type': 'sha', 'salt': 'c' }) self.assertEqual(binascii.hexlify(kd(['a', 'b', 'c'], 13)), 'b87a32912e780ab8e22555d132')
def test_long(self): kd = pol.kd.KeyDerivation.setup({'bits': 256, 'type': 'sha','salt':'c'}) self.assertEqual(binascii.hexlify(kd(['a', 'b', 'c'], 128)), 'b87a32912e780ab8e22555d132fec8c01b2867128ebb4e56dcac029e71ac9'+ '02f9e6c49cc332427586fef3cd34330d2724494c09044f475b7c47c24774b'+ '996059a8fe87e36dde9c60b1e3838d5a891d023f58b73667672d3b796224e'+ '6b7c617bb6b20a9c08b49f40f9b37f5f34be841e957e415638b6cc03cb4c5'+ '2906044e65e5')
def test_single(self): kd = pol.kd.KeyDerivation.setup({ 'bits': 256, 'type': 'sha', 'salt': 'c' }) self.assertEqual( binascii.hexlify(kd(['ab'])), '530c455f8ff55d3d40bac27074c8f730' + '8c7bd40e03808353e3296207ada91388')
def test_single_unsalted(self): kd = pol.kd.KeyDerivation.setup({ 'bits': 256, 'type': 'sha', 'salt': '' }) self.assertEqual( binascii.hexlify(kd(['abc'])), '4be9b856664b17f617ac8f072c48f80f' + '6b008abb9f0499c2a5914b5683f3386f')
def test_multiple_unsalted(self): kd = pol.kd.KeyDerivation.setup({ 'bits': 256, 'type': 'sha', 'salt': '' }) self.assertEqual( binascii.hexlify(kd(['a', 'b', 'c'])), '0a8ffaf1c23bf0fa6e3a9cc1fa4ff914' + '900213a45cba87d66d9d6c611dcf3cee')
def test_multiple_salted(self): kd = pol.kd.KeyDerivation.setup({ 'bits': 256, 'type': 'sha', 'salt': 'c' }) self.assertEqual( binascii.hexlify(kd(['a', 'b', 'c'])), 'b87a32912e780ab8e22555d132fec8c0' + '1b2867128ebb4e56dcac029e71ac902f')
def test_double(self): kd = pol.kd.KeyDerivation.setup({ 'bits': 256, 'type': 'sha', 'salt': 'c' }) self.assertEqual( binascii.hexlify(kd(['a', 'b', 'c'], 64)), 'b87a32912e780ab8e22555d132fec8c01b2867128ebb4e56dcac029e71ac9' + '02f9e6c49cc332427586fef3cd34330d2724494c09044f475b7c47c24774b' + '996059')
def test_long(self): kd = pol.kd.KeyDerivation.setup({ 'bits': 256, 'type': 'sha', 'salt': 'c' }) self.assertEqual( binascii.hexlify(kd(['a', 'b', 'c'], 128)), 'b87a32912e780ab8e22555d132fec8c01b2867128ebb4e56dcac029e71ac9' + '02f9e6c49cc332427586fef3cd34330d2724494c09044f475b7c47c24774b' + '996059a8fe87e36dde9c60b1e3838d5a891d023f58b73667672d3b796224e' + '6b7c617bb6b20a9c08b49f40f9b37f5f34be841e957e415638b6cc03cb4c5' + '2906044e65e5')
def test_double(self): kd = pol.kd.KeyDerivation.setup({'bits': 256, 'type': 'sha','salt':'c'}) self.assertEqual(binascii.hexlify(kd(['a', 'b', 'c'], 64)), 'b87a32912e780ab8e22555d132fec8c01b2867128ebb4e56dcac029e71ac9'+ '02f9e6c49cc332427586fef3cd34330d2724494c09044f475b7c47c24774b'+ '996059')
def test_short(self): kd = pol.kd.KeyDerivation.setup({'bits': 256, 'type': 'sha','salt':'c'}) self.assertEqual(binascii.hexlify(kd(['a', 'b', 'c'], 13)), 'b87a32912e780ab8e22555d132')
def test_multiple_unsalted(self): kd = pol.kd.KeyDerivation.setup({'bits': 256, 'type': 'sha','salt': ''}) self.assertEqual(binascii.hexlify(kd(['a', 'b', 'c'])), '0a8ffaf1c23bf0fa6e3a9cc1fa4ff914'+ '900213a45cba87d66d9d6c611dcf3cee')
def test_multiple_salted(self): kd = pol.kd.KeyDerivation.setup({'bits': 256, 'type': 'sha','salt':'c'}) self.assertEqual(binascii.hexlify(kd(['a', 'b', 'c'])), 'b87a32912e780ab8e22555d132fec8c0'+ '1b2867128ebb4e56dcac029e71ac902f')
def test_single(self): kd = pol.kd.KeyDerivation.setup({'bits': 256, 'type': 'sha','salt':'c'}) self.assertEqual(binascii.hexlify(kd(['ab'])), '530c455f8ff55d3d40bac27074c8f730'+ '8c7bd40e03808353e3296207ada91388')
def test_single_unsalted(self): kd = pol.kd.KeyDerivation.setup({'bits': 256, 'type': 'sha', 'salt':''}) self.assertEqual(binascii.hexlify(kd(['abc'])), '4be9b856664b17f617ac8f072c48f80f'+ '6b008abb9f0499c2a5914b5683f3386f')
def main(program): data = [] kd = pol.kd.KeyDerivation.setup() data.append(('kd.derive (1000x)', timeit.repeat(functools.partial(kd.derive, ['']), repeat=3, number=1000))) ks = pol.ks.KeyStretching.setup() data.append(('ks.stretch', timeit.repeat(functools.partial(ks.stretch, ''), repeat=3, number=1))) bs = pol.blockcipher.BlockCipher.setup() def bs_encrypt(): s = bs.new_stream('!'*32, '!'*16) s.encrypt(' '*20480) data.append(('blockcipher encrypt (500x 20KB)', timeit.repeat(bs_encrypt, repeat=3, number=500))) def bs_decrypt(): s = bs.new_stream('!'*32, '!'*16) s.decrypt(' '*20480) data.append(('blockcipher decrypt (500x 20KB)', timeit.repeat(bs_decrypt, repeat=3, number=500))) randfunc = Crypto.Random.new().read data.append(('random (1000x 64B)', timeit.repeat(functools.partial(randfunc, 64), repeat=3, number=1000))) data.append(('random (5x 1MB)', timeit.repeat(functools.partial(randfunc, 1024*1024), repeat=3, number=5))) gp = pol.elgamal.precomputed_group_params() privkey = pol.elgamal.string_to_group(kd([], length=128)) pubkey = pol.elgamal.pubkey_from_privkey(privkey, gp) c1, c2 = pol.elgamal.encrypt('!'*128, pubkey, gp, 128, randfunc) data.append(('EG pubkey_from_privkey (100x)', timeit.repeat(functools.partial(pol.elgamal.pubkey_from_privkey, privkey, gp), repeat=3, number=100))) data.append(('EG encrypt (100x)', timeit.repeat(functools.partial(pol.elgamal.encrypt, '!'*128, pubkey, gp, 128, randfunc), repeat=3, number=100))) data.append(('EG decrypt (100x)', timeit.repeat(functools.partial(pol.elgamal.decrypt, c1, c2, privkey, gp, 128), repeat=3, number=100))) data.append(('string_to_number (10000x)', timeit.repeat(functools.partial(pol.serialization.string_to_number, '!'*128), repeat=3, number=10000))) number = pol.serialization.string_to_number('!'*128) data.append(('number_to_string (10000x)', timeit.repeat(functools.partial(pol.serialization.number_to_string, number), repeat=3, number=10000))) data.append(('_find_safe_prime', timeit.repeat(functools.partial(pol.elgamal._find_safe_prime, 1025, randfunc), repeat=3, number=1))) envelope = pol.envelope.Envelope.setup() data.append(('envelope gen. keypair (50x)', timeit.repeat(envelope.generate_keypair, repeat=3, number=50))) pubkey, privkey = envelope.generate_keypair() msg = envelope.seal('!', pubkey) data.append(('envelope seal (50x)', timeit.repeat(functools.partial(envelope.seal, '!'*128, pubkey), repeat=3, number=50))) data.append(('envelope open (50x)', timeit.repeat(functools.partial(envelope.open, msg, privkey), repeat=3, number=50))) for desc, res in data: print '%-40s %.4f %.4f %.4f' % (desc, res[0], res[1], res[2])