def test_nth_key(self): self.assertEqual(des.nth_key(765637), unhexlify('01010101015dba8a')) self.assertEqual(des.encrypt(des.nth_key(1), unhexlify('0102030405060708')), unhexlify('6613fc98d6d2f56b')) for j in range(1000): key = des.nth_key(randint(0, 10000000)) for i in range(8): # Assert hamming weight odd for each byte self.assertEqual(bin(key[i]).count('1') % 2, 1)
def test_meet_in_the_middle(self): bits = 8 key1 = des.nth_key(randint(0, 2**bits)) key2 = des.nth_key(randint(0, 2**bits)) pairs = [] for i in range(3): plain_text = bytes((randint(0, 255) for i in range(8))) cipher_text = des.encrypt(key2, des.encrypt(key1, plain_text)) pairs.append((plain_text, cipher_text)) self.assertEqual(des.meet_in_the_middle(bits, pairs), (key1, key2))