Mercurial > projects > dcrypt
annotate dcrypt/crypto/hashes/MD5.d @ 26:176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
author | Thomas Dixon <reikon@reikon.us> |
---|---|
date | Sun, 01 Mar 2009 13:06:48 -0500 |
parents | 4589f8c5eb3c |
children | 8b5eaf3c2979 |
rev | line source |
---|---|
0 | 1 /** |
2 * This file is part of the dcrypt project. | |
3 * | |
4 * Copyright: Copyright (C) dcrypt contributors 2008. All rights reserved. | |
5 * License: MIT | |
6 * Authors: Thomas Dixon | |
7 */ | |
8 | |
9 module dcrypt.crypto.hashes.MD5; | |
10 | |
11 public import dcrypt.crypto.Hash; | |
12 | |
6
5cb17e09d685
Minor edits to the unittests of hash functions and ciphers. Added AES and test vectors.
Thomas Dixon <reikon@reikon.us>
parents:
3
diff
changeset
|
13 /** |
5cb17e09d685
Minor edits to the unittests of hash functions and ciphers. Added AES and test vectors.
Thomas Dixon <reikon@reikon.us>
parents:
3
diff
changeset
|
14 * Implementation of Ron Rivest's MD5. |
5cb17e09d685
Minor edits to the unittests of hash functions and ciphers. Added AES and test vectors.
Thomas Dixon <reikon@reikon.us>
parents:
3
diff
changeset
|
15 * |
5cb17e09d685
Minor edits to the unittests of hash functions and ciphers. Added AES and test vectors.
Thomas Dixon <reikon@reikon.us>
parents:
3
diff
changeset
|
16 * Conforms: RFC 1321 |
5cb17e09d685
Minor edits to the unittests of hash functions and ciphers. Added AES and test vectors.
Thomas Dixon <reikon@reikon.us>
parents:
3
diff
changeset
|
17 * References: http://www.faqs.org/rfcs/rfc1321.html |
5cb17e09d685
Minor edits to the unittests of hash functions and ciphers. Added AES and test vectors.
Thomas Dixon <reikon@reikon.us>
parents:
3
diff
changeset
|
18 * Bugs: MD5 is not cryptographically secure. |
5cb17e09d685
Minor edits to the unittests of hash functions and ciphers. Added AES and test vectors.
Thomas Dixon <reikon@reikon.us>
parents:
3
diff
changeset
|
19 */ |
0 | 20 class MD5 : Hash { |
21 private uint h0, h1, h2, h3; | |
22 | |
23 // Shift amounts | |
24 private enum { | |
25 S11 = 7, | |
26 S12 = 12, | |
27 S13 = 17, | |
28 S14 = 22, | |
29 | |
30 S21 = 5, | |
31 S22 = 9, | |
32 S23 = 14, | |
33 S24 = 20, | |
34 | |
35 S31 = 4, | |
36 S32 = 11, | |
37 S33 = 16, | |
38 S34 = 23, | |
39 | |
40 S41 = 6, | |
41 S42 = 10, | |
42 S43 = 15, | |
43 S44 = 21 | |
44 }; | |
45 | |
46 this (void[] input_=null) { | |
47 reset(); | |
48 super(input_); | |
49 } | |
50 | |
51 uint blockSize() { | |
52 return 64; | |
53 } | |
54 | |
55 uint digestSize() { | |
56 return 16; | |
57 } | |
58 | |
59 char[] name() { | |
60 return "MD5"; | |
61 } | |
62 | |
63 void transform(ubyte[] input) { | |
64 uint[] w = new uint[16]; | |
65 | |
23
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
10
diff
changeset
|
66 for (int i = 0, j = 0; i < 16; i++,j+=int.sizeof) |
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
10
diff
changeset
|
67 w[i] = ByteConverter.LittleEndian.to!(uint)(input[j..j+int.sizeof]); |
0 | 68 |
69 uint a = h0, | |
70 b = h1, | |
71 c = h2, | |
72 d = h3; | |
73 | |
74 // Round 1 -- FIGHT! | |
26
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
75 ff(a, b, c, d, w[ 0], S11, 0xd76aa478u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
76 ff(d, a, b, c, w[ 1], S12, 0xe8c7b756u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
77 ff(c, d, a, b, w[ 2], S13, 0x242070dbu); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
78 ff(b, c, d, a, w[ 3], S14, 0xc1bdceeeu); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
79 ff(a, b, c, d, w[ 4], S11, 0xf57c0fafu); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
80 ff(d, a, b, c, w[ 5], S12, 0x4787c62au); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
81 ff(c, d, a, b, w[ 6], S13, 0xa8304613u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
82 ff(b, c, d, a, w[ 7], S14, 0xfd469501u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
83 ff(a, b, c, d, w[ 8], S11, 0x698098d8u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
84 ff(d, a, b, c, w[ 9], S12, 0x8b44f7afu); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
85 ff(c, d, a, b, w[10], S13, 0xffff5bb1u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
86 ff(b, c, d, a, w[11], S14, 0x895cd7beu); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
87 ff(a, b, c, d, w[12], S11, 0x6b901122u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
88 ff(d, a, b, c, w[13], S12, 0xfd987193u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
89 ff(c, d, a, b, w[14], S13, 0xa679438eu); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
90 ff(b, c, d, a, w[15], S14, 0x49b40821u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
91 |
0 | 92 // Round 2 |
26
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
93 gg(a, b, c, d, w[ 1], S21, 0xf61e2562u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
94 gg(d, a, b, c, w[ 6], S22, 0xc040b340u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
95 gg(c, d, a, b, w[11], S23, 0x265e5a51u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
96 gg(b, c, d, a, w[ 0], S24, 0xe9b6c7aau); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
97 gg(a, b, c, d, w[ 5], S21, 0xd62f105du); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
98 gg(d, a, b, c, w[10], S22, 0x02441453u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
99 gg(c, d, a, b, w[15], S23, 0xd8a1e681u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
100 gg(b, c, d, a, w[ 4], S24, 0xe7d3fbc8u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
101 gg(a, b, c, d, w[ 9], S21, 0x21e1cde6u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
102 gg(d, a, b, c, w[14], S22, 0xc33707d6u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
103 gg(c, d, a, b, w[ 3], S23, 0xf4d50d87u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
104 gg(b, c, d, a, w[ 8], S24, 0x455a14edu); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
105 gg(a, b, c, d, w[13], S21, 0xa9e3e905u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
106 gg(d, a, b, c, w[ 2], S22, 0xfcefa3f8u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
107 gg(c, d, a, b, w[ 7], S23, 0x676f02d9u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
108 gg(b, c, d, a, w[12], S24, 0x8d2a4c8au); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
109 |
0 | 110 // Round 3 |
26
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
111 hh(a, b, c, d, w[ 5], S31, 0xfffa3942u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
112 hh(d, a, b, c, w[ 8], S32, 0x8771f681u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
113 hh(c, d, a, b, w[11], S33, 0x6d9d6122u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
114 hh(b, c, d, a, w[14], S34, 0xfde5380cu); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
115 hh(a, b, c, d, w[ 1], S31, 0xa4beea44u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
116 hh(d, a, b, c, w[ 4], S32, 0x4bdecfa9u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
117 hh(c, d, a, b, w[ 7], S33, 0xf6bb4b60u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
118 hh(b, c, d, a, w[10], S34, 0xbebfbc70u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
119 hh(a, b, c, d, w[13], S31, 0x289b7ec6u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
120 hh(d, a, b, c, w[ 0], S32, 0xeaa127fau); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
121 hh(c, d, a, b, w[ 3], S33, 0xd4ef3085u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
122 hh(b, c, d, a, w[ 6], S34, 0x04881d05u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
123 hh(a, b, c, d, w[ 9], S31, 0xd9d4d039u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
124 hh(d, a, b, c, w[12], S32, 0xe6db99e5u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
125 hh(c, d, a, b, w[15], S33, 0x1fa27cf8u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
126 hh(b, c, d, a, w[ 2], S34, 0xc4ac5665u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
127 |
0 | 128 // Round 4 |
26
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
129 ii(a, b, c, d, w[ 0], S41, 0xf4292244u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
130 ii(d, a, b, c, w[ 7], S42, 0x432aff97u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
131 ii(c, d, a, b, w[14], S43, 0xab9423a7u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
132 ii(b, c, d, a, w[ 5], S44, 0xfc93a039u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
133 ii(a, b, c, d, w[12], S41, 0x655b59c3u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
134 ii(d, a, b, c, w[ 3], S42, 0x8f0ccc92u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
135 ii(c, d, a, b, w[10], S43, 0xffeff47du); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
136 ii(b, c, d, a, w[ 1], S44, 0x85845dd1u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
137 ii(a, b, c, d, w[ 8], S41, 0x6fa87e4fu); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
138 ii(d, a, b, c, w[15], S42, 0xfe2ce6e0u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
139 ii(c, d, a, b, w[ 6], S43, 0xa3014314u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
140 ii(b, c, d, a, w[13], S44, 0x4e0811a1u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
141 ii(a, b, c, d, w[ 4], S41, 0xf7537e82u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
142 ii(d, a, b, c, w[11], S42, 0xbd3af235u); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
143 ii(c, d, a, b, w[ 2], S43, 0x2ad7d2bbu); |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
144 ii(b, c, d, a, w[ 9], S44, 0xeb86d391u); |
0 | 145 |
146 // FINISH HIM! | |
147 h0 += a; | |
148 h1 += b; | |
149 h2 += c; | |
150 h3 += d; | |
26
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
151 // FATALITY! \o/ |
0 | 152 } |
153 | |
154 private uint f(uint x, uint y, uint z) { | |
26
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
155 return (x&y)|(~x&z); |
0 | 156 } |
157 | |
158 private uint h(uint x, uint y, uint z) { | |
26
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
159 return x^y^z; |
0 | 160 } |
161 | |
162 private uint g(uint x, uint y, uint z) { | |
163 return (x&z)|(y&~z); | |
164 } | |
165 | |
166 private uint i(uint x, uint y, uint z) { | |
167 return y^(x|~z); | |
168 } | |
169 | |
170 private void ff(ref uint a, uint b, uint c, | |
171 uint d, uint x, uint s, uint ac) { | |
172 a += f(b, c, d) + x + ac; | |
23
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
10
diff
changeset
|
173 a = Bitwise.rotateLeft(a, s); |
0 | 174 a += b; |
175 } | |
176 | |
177 private void gg(ref uint a, uint b, uint c, | |
178 uint d, uint x, uint s, uint ac) { | |
179 a += g(b, c, d) + x + ac; | |
23
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
10
diff
changeset
|
180 a = Bitwise.rotateLeft(a, s); |
0 | 181 a += b; |
182 } | |
183 | |
184 private void hh(ref uint a, uint b, uint c, | |
185 uint d, uint x, uint s, uint ac) { | |
186 a += h(b, c, d) + x + ac; | |
23
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
10
diff
changeset
|
187 a = Bitwise.rotateLeft(a, s); |
0 | 188 a += b; |
189 } | |
190 | |
191 private void ii(ref uint a, uint b, uint c, | |
192 uint d, uint x, uint s, uint ac) { | |
193 a += i(b, c, d) + x + ac; | |
23
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
10
diff
changeset
|
194 a = Bitwise.rotateLeft(a, s); |
0 | 195 a += b; |
196 } | |
197 | |
198 ubyte[] digest() { | |
199 padMessage(MODE_MD); | |
200 ubyte[] result = new ubyte[digestSize]; | |
201 | |
23
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
10
diff
changeset
|
202 result[0..4] = ByteConverter.LittleEndian.from!(uint)(h0); |
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
10
diff
changeset
|
203 result[4..8] = ByteConverter.LittleEndian.from!(uint)(h1); |
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
10
diff
changeset
|
204 result[8..12] = ByteConverter.LittleEndian.from!(uint)(h2); |
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
10
diff
changeset
|
205 result[12..16] = ByteConverter.LittleEndian.from!(uint)(h3); |
0 | 206 |
207 reset(); | |
208 return result; | |
209 } | |
210 | |
211 void reset() { | |
212 super.reset(); | |
26
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
213 h0 = 0x67452301u; |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
214 h1 = 0xefcdab89u; |
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
215 h2 = 0x98badcfeu; |
0 | 216 h3 = 0x10325476u; |
217 } | |
218 | |
2
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
219 MD5 copy() { |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
220 MD5 h = new MD5(buffer[0..index]); |
3
a5789a7b3b3b
Fixed error in hash copy() functions where I forgot to copy the length of the message. Now, twice as jank! =)
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
221 h.bytes = bytes; |
2
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
222 h.h0 = h0; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
223 h.h1 = h1; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
224 h.h2 = h2; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
225 h.h3 = h3; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
226 return h; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
227 } |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
228 |
26
176c933827a8
Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
229 debug (UnitTest) { |
0 | 230 // Found in Tango <3 |
231 unittest { | |
232 static const char[][] test_inputs = [ | |
233 "", | |
234 "a", | |
235 "abc", | |
236 "message digest", | |
237 "abcdefghijklmnopqrstuvwxyz", | |
238 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", | |
239 "12345678901234567890123456789012345678901234567890123456789012345678901234567890" | |
240 ]; | |
241 | |
242 static const char[][] test_results = [ | |
243 "d41d8cd98f00b204e9800998ecf8427e", | |
244 "0cc175b9c0f1b6a831c399e269772661", | |
245 "900150983cd24fb0d6963f7d28e17f72", | |
246 "f96b697d7cb7938d525a2f31aaf161d0", | |
247 "c3fcd3d76192e4007dfb496cca67e13b", | |
248 "d174ab98d277d9f5a5611c2c9f419d9f", | |
249 "57edf4a22be3c955ac49da2e2107b67a" | |
250 ]; | |
251 | |
252 MD5 h = new MD5(); | |
253 foreach (uint i, char[] input; test_inputs) { | |
254 h.update(input); | |
255 char[] digest = h.hexDigest(); | |
256 assert(digest == test_results[i], | |
2
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
257 h.name~": ("~digest~") != ("~test_results[i]~")"); |
0 | 258 } |
259 } | |
260 } | |
261 } |