def test_hmac(self): keys = [b'', b'key'] messages = [b'', b'The quick brown fox jumps over the lazy dog'] digests = [ cu.hex_to_bytes('fbdb1d1b18aa6c08324b7d64b71fb76370690e1d'), cu.hex_to_bytes('de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9') ] self.assertTrue( all([ ch.HMAC(k, m) == d for k, m, d in zip(keys, messages, digests) ]))
def test_sha_1(self): messages = [ b'The quick brown fox jumps over the lazy dog', b'The quick brown fox jumps over the lazy cog', b'' ] digests = [ cu.hex_to_bytes('2fd4e1c67a2d28fced849ee1bb76e7391b93eb12'), cu.hex_to_bytes('de9f2c7fd25e1b3afad3e85a0bd17d9b100db4b3'), cu.hex_to_bytes('da39a3ee5e6b4b0d3255bfef95601890afd80709') ] self.assertTrue( all([ch.SHA1(m) == d for m, d in zip(messages, digests)]))
def test_3(self): cipher = cu.hex_to_bytes( '1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736' ) plain_text = "Cooking MC's like a pound of bacon" key_byte = 88 _, plain, key = cs.decrypt_single_byte_XOR(cipher) self.assertEqual(plain.decode('utf-8'), plain_text) self.assertEqual(key[0], key_byte)
def test_5(self): plain_bytes = b"Burning 'em, if you ain't quick and nimble\n" \ +b'I go crazy when I hear a cymbal' key_bytes = b'ICE' expected_bytes = cu.hex_to_bytes( '0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324' \ +'272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b202831652' \ +'86326302e27282f') cipher_bytes = cu.XOR_bytes(plain_bytes, key_bytes) self.assertEqual(cipher_bytes, expected_bytes)
def verify_hmac(): if request.method == 'GET': file = request.args.get('file').encode() signature = hex_to_bytes(request.args.get('signature')) file_sig = HMAC(SECRET_KEY, file) if insecure_compare(signature[:HMAC_LEN], file_sig[:HMAC_LEN]): return 'OK', 200 else: return 'BAD', 500
def test_4(self): plain_expect = 'Now that the party is jumping\n' best_prob, best_plain = 0, '' with open('data/Set_1_4.txt', 'r') as f: for line in f: cipher = cu.hex_to_bytes(line) prob, plain, _ = cs.decrypt_single_byte_XOR(cipher) if prob > best_prob: best_prob = prob best_plain = plain.decode('utf-8') self.assertEqual(best_plain, plain_expect)
def test_md4(self): messages = [ b'', b'a', b'abc', b'message digest', b'abcdefghijklmnopqrstuvwxyz', b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', b'12345678901234567890123456789012345678901234567890123456789012345678901234567890' ] digests = [ cu.hex_to_bytes('31d6cfe0d16ae931b73c59d7e0c089c0'), cu.hex_to_bytes('bde52cb31de33e46245e05fbdbd6fb24'), cu.hex_to_bytes('a448017aaf21d8525fc10ae87aa6729d'), cu.hex_to_bytes('d9130a8164549fe818874806e1c7014b'), cu.hex_to_bytes('d79e1c308aa5bbcdeea8ed63df412da9'), cu.hex_to_bytes('043f8582f241db351ce627e153e7f0e4'), cu.hex_to_bytes('e33b4ddc9c38f2199c3e7b164fcc0536') ] self.assertTrue( all([ch.MD4(m) == d for m, d in zip(messages, digests)]))
def test_43(self): public = 0x84ad4719d044495496a3201c8ff484feb45b962e7302e56a392aee4abab3e4bdebf2955b4736012f21a08084056b19bcd7fee56048e004e44984e2f411788efdc837a0d2e5abb7b555039fd243ac01f0fb2ed1dec568280ce678e931868d23eb095fde9d3779191b8c0299d6e07bbb283e6633451e535c45513b2d33c99ea17 message = b'For those that envy a MC it can be hazardous to your health\n' \ +b'So be friendly, a matter of life and death, just like a etch-a-sketch\n' message_hash = 0xd2d0714f014a9784047eaeccf956520045c45265 signature = (548099063082341131477253921760299949438196259240, 857042759984254168557880549501802188789837994940) private_hash = cu.hex_to_bytes( '0954edd5e0afe5542a4adf012611a91912a3ec16') private = None for k in range(2**16): private_guess = ck.recover_DSA_private(message_hash, signature, k) public_guess = pow(ck.DSA_G, private_guess, ck.DSA_P) if public_guess == public: private = private_guess break m = sha1() m.update(hex(private)[2:].encode()) private_hash = m.digest() self.assertEqual(cu.bytes_to_hex(private_hash), '0954edd5e0afe5542a4adf012611a91912a3ec16')
def test_2(self): bytes_1 = cu.hex_to_bytes('1c0111001f010100061a024b53535009181c') bytes_2 = cu.hex_to_bytes('686974207468652062756c6c277320657965') bytes_result = cu.hex_to_bytes('746865206b696420646f6e277420706c6179') self.assertEqual(cu.XOR_bytes(bytes_1, bytes_2), bytes_result)
def test_byte_to_hex_conversion(self): hex_string = '2ef0' byte_array = bytes([46, 240]) self.assertEqual(cu.hex_to_bytes(hex_string), byte_array) self.assertEqual(cu.bytes_to_hex(byte_array), hex_string)