class Md4_u(Utility): """utility class for L{MD4<Md4>} cryptographic hash""" constants = [hsqrt(i) for i in [0, 2, 3]] shifts = [[3, 7, 11, 19], [3, 5, 9, 13], [3, 9, 11, 15]] r = [ [ i, ((i * 4) + (i / 4)) % 16, # 0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15 [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15][i] ] for i in range(16) ] @staticmethod def f(b, c, d): return (b & c) | ((~b) & d) @staticmethod def g(b, c, d): return (b & c) | (b & d) | (c & d) @staticmethod def h(b, c, d): return (b ^ c ^ d) hex = "0123456789ABCDEF" IVhex = hex + hex[::-1] IVs = dwords(struct.unpack("<4L", hex2bin(IVhex)))
def __init__(self): Md4.__init__(self) self.hv_size = 64 hex_ = "0123456789ABCDEF" IVhex = hex_ + hex_[::-1] self.IVs = qwords(list(struct.unpack(">2Q", hex2bin(IVhex))) + [0xF090A0B0C0B0E080 | 0x0006050403020107]) self.pad_bit_7 = False self.S = tiger_const.Ss_test self.nb_pass = 3 # standard tiger uses 3 passes self.sboxes = self.S self.Sboxes = self.S[::-1]
def __init__(self): Md4.__init__(self) self.hv_size = 64 hex_ = "0123456789ABCDEF" IVhex = hex_ + hex_[::-1] self.IVs = qwords( list(struct.unpack(">2Q", hex2bin(IVhex))) + [0xF090A0B0C0B0E080 | 0x0006050403020107]) self.pad_bit_7 = False self.S = tiger_const.Ss_test self.nb_pass = 3 # standard tiger uses 3 passes self.sboxes = self.S self.Sboxes = self.S[::-1]
0x043f8582f241db351ce627e153e7f0e4, 0xe33b4ddc9c38f2199c3e7b164fcc0536] # collision, XiaoyunWang, Dengguo Feng, Xuejia Lai, Hongbo Yu, 2004 a4 = \ "839c7a4d 7a92cb56 78a5d5b9 eea5a757 3c8a74de b366c3dc 20a083b6 9f5d2a3b"\ "b3719dc6 9891e9f9 5e809fd7 e8b23ba6 318edd45 e51fe397 08bf9427 e9c3e8b9" delta4 = \ "_______________d________2______________________________________________"\ "_________________________________________c_____________________________" b4 = add_string(a4, delta4) b4 = b4.replace(" ","") a4, b4 = [hex2bin(s) for s in [a4, b4]] assert Md4_u.IVs == md4_IVs, "md4 IVs" check_test_vectors(hashmd4, md4_test_vectors, "MD4") assert hashmd4(a4) == hashmd4(b4) # MD5 hashmd5 = lambda x: Md5().compute(x).digest() md5_test_vectors = [ 0xd41d8cd98f00b204e9800998ecf8427e, 0x0cc175b9c0f1b6a831c399e269772661, 0x900150983cd24fb0d6963f7d28e17f72,
"6edfc501 37e69330 be976012 cc5dfe1c 14c4c68b d1db3ecb 24438a59 a09b5db4"\ "35563e0d 8bdf572f 77b53065 cef31f32 dc9dbaa0 4146261e 9994bd5c d0758e3d" delta = \ "__________________________________1_______d_________1_b________d_______"\ "a______5_______f___________3_____2__a______d________0_3_____e__7______c"\ "6______d_6_____7_________e_4________d________e_________________________"\ "________________1_______6_________d_1________a________3______1_______c_"\ "7_____f_________0_5______d_a_____d__7______f_6______1_7_____f__c_______"\ "a_____1__9______6__________e______6_a_____9_________e_2______7_a_____8_"\ "_______3__________3______0_4_____5_________9_5________a________2_______"\ "__________________________________0_______e_________c_1________5_______" b = add_string(a, delta) b = b.replace(" ","") a, b = [hex2bin(s) for s in [a, b]] assert hash0(a) == hash0(b), "sha-0 collision" #sha-1 hash1 = lambda x: Sha1().compute(x).digest() sha1_test_vectors = [ 0xDA39A3EE5E6B4B0D3255BFEF95601890AFD80709, 0x86F7E437FAA5A7FCE15D1DDCB9EAEAEA377667B8, 0xA9993E364706816ABA3E25717850C26C9CD0D89D, 0xC12252CEDA8BE8994D5FA0290A47231C1D16AAE3, 0x32D10C7B8CF96570CA04CE37F2A19D84240D3A89, 0x761C457BF73B14D27E9E9265C46F4B4DDA11F940, 0x50ABF5706A150990A08B2C5EA40FA0E585554732]
"6edfc501 37e69330 be976012 cc5dfe1c 14c4c68b d1db3ecb 24438a59 a09b5db4"\ "35563e0d 8bdf572f 77b53065 cef31f32 dc9dbaa0 4146261e 9994bd5c d0758e3d" delta = \ "__________________________________1_______d_________1_b________d_______"\ "a______5_______f___________3_____2__a______d________0_3_____e__7______c"\ "6______d_6_____7_________e_4________d________e_________________________"\ "________________1_______6_________d_1________a________3______1_______c_"\ "7_____f_________0_5______d_a_____d__7______f_6______1_7_____f__c_______"\ "a_____1__9______6__________e______6_a_____9_________e_2______7_a_____8_"\ "_______3__________3______0_4_____5_________9_5________a________2_______"\ "__________________________________0_______e_________c_1________5_______" b = add_string(a, delta) b = b.replace(" ", "") a, b = [hex2bin(s) for s in [a, b]] assert hash0(a) == hash0(b), "sha-0 collision" #sha-1 hash1 = lambda x: Sha1().compute(x).digest() sha1_test_vectors = [ 0xDA39A3EE5E6B4B0D3255BFEF95601890AFD80709, 0x86F7E437FAA5A7FCE15D1DDCB9EAEAEA377667B8, 0xA9993E364706816ABA3E25717850C26C9CD0D89D, 0xC12252CEDA8BE8994D5FA0290A47231C1D16AAE3, 0x32D10C7B8CF96570CA04CE37F2A19D84240D3A89, 0x761C457BF73B14D27E9E9265C46F4B4DDA11F940, 0x50ABF5706A150990A08B2C5EA40FA0E585554732 ]
assert getbinstr(0) == "0" assert getbinstr(8) == "1000" assert countmissing(0, 8) == 0 assert countmissing(3, 8) == 5 assert countmissing(8, 8) == 0 assert insert_string("abcd", 2, "1") == "ab1cd" assert zip([1, 2], [0]) == [(1, 0)] assert zip_extend([1, 2], [0], 0) == [(1, 0), (2, 0)] assert zip_extend([0], [1, 2], 0) == [(1, 0), (2, 0)] assert rorstr("abc") == "cab" assert [rorstr("abc", i) for i in range(4)] == ['abc', 'cab', 'bca', 'abc'] assert setstr("abcde","d") == "deabc" assert list(slice_and_pad("0001" + "0000" + "0", 4)) == ['0001', '0000', '01__'] assert list(slice_and_pad("0001" + "0000" + "00", 4)) == ['0001', '0000', '001_'] assert list(slice_and_pad("0001" + "0000" + "000", 4)) == ['0001', '0000', '0001', 'extr'] assert list(slice_and_pad("0001" + "0000" + "0000", 4)) == ['0001', '0000', '0000', '1___'] assert hex2bin("0123456789ABCDEF") == "\x01\x23\x45\x67\x89\xAB\xCD\xEF" assert "0123456789ABCDEF" == bin2hex("\x01\x23\x45\x67\x89\xAB\xCD\xEF") assert list(struct.unpack(">16L", ASCII[:64])) == as_words(ASCII[:64], 512, 32, True)
assert countmissing(0, 8) == 0 assert countmissing(3, 8) == 5 assert countmissing(8, 8) == 0 assert insert_string("abcd", 2, "1") == "ab1cd" assert zip([1, 2], [0]) == [(1, 0)] assert zip_extend([1, 2], [0], 0) == [(1, 0), (2, 0)] assert zip_extend([0], [1, 2], 0) == [(1, 0), (2, 0)] assert rorstr("abc") == "cab" assert [rorstr("abc", i) for i in range(4)] == ['abc', 'cab', 'bca', 'abc'] assert setstr("abcde", "d") == "deabc" assert list(slice_and_pad("0001" + "0000" + "0", 4)) == ['0001', '0000', '01__'] assert list(slice_and_pad("0001" + "0000" + "00", 4)) == ['0001', '0000', '001_'] assert list(slice_and_pad("0001" + "0000" + "000", 4)) == ['0001', '0000', '0001', 'extr'] assert list(slice_and_pad("0001" + "0000" + "0000", 4)) == ['0001', '0000', '0000', '1___'] assert hex2bin("0123456789ABCDEF") == "\x01\x23\x45\x67\x89\xAB\xCD\xEF" assert "0123456789ABCDEF" == bin2hex("\x01\x23\x45\x67\x89\xAB\xCD\xEF") assert list(struct.unpack(">16L", ASCII[:64])) == as_words(ASCII[:64], 512, 32, True)
0xe33b4ddc9c38f2199c3e7b164fcc0536 ] # collision, XiaoyunWang, Dengguo Feng, Xuejia Lai, Hongbo Yu, 2004 a4 = \ "839c7a4d 7a92cb56 78a5d5b9 eea5a757 3c8a74de b366c3dc 20a083b6 9f5d2a3b"\ "b3719dc6 9891e9f9 5e809fd7 e8b23ba6 318edd45 e51fe397 08bf9427 e9c3e8b9" delta4 = \ "_______________d________2______________________________________________"\ "_________________________________________c_____________________________" b4 = add_string(a4, delta4) b4 = b4.replace(" ", "") a4, b4 = [hex2bin(s) for s in [a4, b4]] assert Md4_u.IVs == md4_IVs, "md4 IVs" check_test_vectors(hashmd4, md4_test_vectors, "MD4") assert hashmd4(a4) == hashmd4(b4) # MD5 hashmd5 = lambda x: Md5().compute(x).digest() md5_test_vectors = [ 0xd41d8cd98f00b204e9800998ecf8427e, 0x0cc175b9c0f1b6a831c399e269772661, 0x900150983cd24fb0d6963f7d28e17f72, 0xf96b697d7cb7938d525a2f31aaf161d0, 0xc3fcd3d76192e4007dfb496cca67e13b, 0xd174ab98d277d9f5a5611c2c9f419d9f, 0x57edf4a22be3c955ac49da2e2107b67a ]