Mercurial > projects > dcrypt
annotate dcrypt/crypto/hashes/SHA1.d @ 28:ad687db713a4
Further reworked the code for hash padding. Replaced all instances of 'char[]' with 'string' and removed a few 'const' modifiers as per Glenn Haecker's patch for D2 compatibility. Updated CONTRIBUTORS file.
author | Thomas Dixon <reikon@reikon.us> |
---|---|
date | Sun, 10 May 2009 22:38:48 -0400 |
parents | 8b5eaf3c2979 |
children |
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.SHA1; | |
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 the US NSA's SHA-1. |
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: FIPS 180-1 |
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.itl.nist.gov/fipspubs/fip180-1.htm |
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: SHA-1 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 */ |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
20 class SHA1 : Hash |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
21 { |
28
ad687db713a4
Further reworked the code for hash padding. Replaced all instances of 'char[]' with 'string' and removed a few 'const' modifiers as per Glenn Haecker's patch for D2 compatibility. Updated CONTRIBUTORS file.
Thomas Dixon <reikon@reikon.us>
parents:
27
diff
changeset
|
22 protected uint h0, h1, h2, h3, h4; |
0 | 23 |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
24 this (void[] input_=null) |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
25 { |
0 | 26 reset(); |
27 super(input_); | |
28 } | |
29 | |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
30 uint blockSize() |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
31 { |
0 | 32 return 64; |
33 } | |
34 | |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
35 uint digestSize() |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
36 { |
0 | 37 return 20; |
38 } | |
39 | |
28
ad687db713a4
Further reworked the code for hash padding. Replaced all instances of 'char[]' with 'string' and removed a few 'const' modifiers as per Glenn Haecker's patch for D2 compatibility. Updated CONTRIBUTORS file.
Thomas Dixon <reikon@reikon.us>
parents:
27
diff
changeset
|
40 string name() |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
41 { |
0 | 42 return "SHA1"; |
43 } | |
44 | |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
45 void transform(ubyte[] input) |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
46 { |
0 | 47 uint[] w = new uint[80]; |
48 | |
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:
6
diff
changeset
|
49 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:
6
diff
changeset
|
50 w[i] = ByteConverter.BigEndian.to!(uint)(input[j..j+int.sizeof]); |
0 | 51 |
52 for (int i = 16; i < 80; i++) | |
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:
6
diff
changeset
|
53 w[i] = Bitwise.rotateLeft(w[i-3]^w[i-8]^w[i-14]^w[i-16], 1); |
0 | 54 |
55 uint a = h0, | |
56 b = h1, | |
57 c = h2, | |
58 d = h3, | |
59 e = h4; | |
60 | |
61 int i = 0; | |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
62 for (; i < 20;) |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
63 { |
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:
6
diff
changeset
|
64 e += Bitwise.rotateLeft(a, 5) + f0(b, c, d) + w[i++]; |
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:
6
diff
changeset
|
65 b = Bitwise.rotateLeft(b, 30); |
0 | 66 |
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:
6
diff
changeset
|
67 d += Bitwise.rotateLeft(e, 5) + f0(a, b, c) + w[i++]; |
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:
6
diff
changeset
|
68 a = Bitwise.rotateLeft(a, 30); |
0 | 69 |
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:
6
diff
changeset
|
70 c += Bitwise.rotateLeft(d, 5) + f0(e, a, b) + w[i++]; |
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:
6
diff
changeset
|
71 e = Bitwise.rotateLeft(e, 30); |
0 | 72 |
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:
6
diff
changeset
|
73 b += Bitwise.rotateLeft(c, 5) + f0(d, e, a) + w[i++]; |
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:
6
diff
changeset
|
74 d = Bitwise.rotateLeft(d, 30); |
0 | 75 |
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:
6
diff
changeset
|
76 a += Bitwise.rotateLeft(b, 5) + f0(c, d, e) + w[i++]; |
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:
6
diff
changeset
|
77 c = Bitwise.rotateLeft(c, 30); |
0 | 78 } |
79 | |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
80 for (; i < 40;) |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
81 { |
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:
6
diff
changeset
|
82 e += Bitwise.rotateLeft(a, 5) + f1(b, c, d) + w[i++]; |
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:
6
diff
changeset
|
83 b = Bitwise.rotateLeft(b, 30); |
0 | 84 |
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:
6
diff
changeset
|
85 d += Bitwise.rotateLeft(e, 5) + f1(a, b, c) + w[i++]; |
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:
6
diff
changeset
|
86 a = Bitwise.rotateLeft(a, 30); |
0 | 87 |
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:
6
diff
changeset
|
88 c += Bitwise.rotateLeft(d, 5) + f1(e, a, b) + w[i++]; |
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:
6
diff
changeset
|
89 e = Bitwise.rotateLeft(e, 30); |
0 | 90 |
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:
6
diff
changeset
|
91 b += Bitwise.rotateLeft(c, 5) + f1(d, e, a) + w[i++]; |
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:
6
diff
changeset
|
92 d = Bitwise.rotateLeft(d, 30); |
0 | 93 |
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:
6
diff
changeset
|
94 a += Bitwise.rotateLeft(b, 5) + f1(c, d, e) + w[i++]; |
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:
6
diff
changeset
|
95 c = Bitwise.rotateLeft(c, 30); |
0 | 96 } |
97 | |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
98 for (; i < 60;) |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
99 { |
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:
6
diff
changeset
|
100 e += Bitwise.rotateLeft(a, 5) + f2(b, c, d) + w[i++]; |
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:
6
diff
changeset
|
101 b = Bitwise.rotateLeft(b, 30); |
0 | 102 |
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:
6
diff
changeset
|
103 d += Bitwise.rotateLeft(e, 5) + f2(a, b, c) + w[i++]; |
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:
6
diff
changeset
|
104 a = Bitwise.rotateLeft(a, 30); |
0 | 105 |
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:
6
diff
changeset
|
106 c += Bitwise.rotateLeft(d, 5) + f2(e, a, b) + w[i++]; |
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:
6
diff
changeset
|
107 e = Bitwise.rotateLeft(e, 30); |
0 | 108 |
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:
6
diff
changeset
|
109 b += Bitwise.rotateLeft(c, 5) + f2(d, e, a) + w[i++]; |
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:
6
diff
changeset
|
110 d = Bitwise.rotateLeft(d, 30); |
0 | 111 |
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:
6
diff
changeset
|
112 a += Bitwise.rotateLeft(b, 5) + f2(c, d, e) + w[i++]; |
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:
6
diff
changeset
|
113 c = Bitwise.rotateLeft(c, 30); |
0 | 114 } |
115 | |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
116 for (; i < 80;) |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
117 { |
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:
6
diff
changeset
|
118 e += Bitwise.rotateLeft(a, 5) + f3(b, c, d) + w[i++]; |
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:
6
diff
changeset
|
119 b = Bitwise.rotateLeft(b, 30); |
0 | 120 |
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:
6
diff
changeset
|
121 d += Bitwise.rotateLeft(e, 5) + f3(a, b, c) + w[i++]; |
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:
6
diff
changeset
|
122 a = Bitwise.rotateLeft(a, 30); |
0 | 123 |
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:
6
diff
changeset
|
124 c += Bitwise.rotateLeft(d, 5) + f3(e, a, b) + w[i++]; |
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:
6
diff
changeset
|
125 e = Bitwise.rotateLeft(e, 30); |
0 | 126 |
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:
6
diff
changeset
|
127 b += Bitwise.rotateLeft(c, 5) + f3(d, e, a) + w[i++]; |
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:
6
diff
changeset
|
128 d = Bitwise.rotateLeft(d, 30); |
0 | 129 |
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:
6
diff
changeset
|
130 a += Bitwise.rotateLeft(b, 5) + f3(c, d, e) + w[i++]; |
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:
6
diff
changeset
|
131 c = Bitwise.rotateLeft(c, 30); |
0 | 132 } |
133 | |
134 h0 += a; | |
135 h1 += b; | |
136 h2 += c; | |
137 h3 += d; | |
138 h4 += e; | |
139 } | |
140 | |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
141 private uint f0(uint x, uint y, uint z) |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
142 { |
0 | 143 return (z^(x&(y^z))) + 0x5a827999; |
144 } | |
145 | |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
146 private uint f1(uint x, uint y, uint z) |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
147 { |
0 | 148 return (x^y^z) + 0x6ed9eba1; |
149 } | |
150 | |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
151 private uint f2(uint x, uint y, uint z) |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
152 { |
0 | 153 return ((x&y)|(z&(x|y))) + 0x8f1bbcdc; |
154 } | |
155 | |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
156 private uint f3(uint x, uint y, uint z) |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
157 { |
0 | 158 return (x^y^z) + 0xca62c1d6; |
159 } | |
160 | |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
161 ubyte[] digest() |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
162 { |
28
ad687db713a4
Further reworked the code for hash padding. Replaced all instances of 'char[]' with 'string' and removed a few 'const' modifiers as per Glenn Haecker's patch for D2 compatibility. Updated CONTRIBUTORS file.
Thomas Dixon <reikon@reikon.us>
parents:
27
diff
changeset
|
163 padMessage(MODE_SHA); |
0 | 164 ubyte[] result = new ubyte[digestSize]; |
165 | |
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:
6
diff
changeset
|
166 result[0..4] = ByteConverter.BigEndian.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:
6
diff
changeset
|
167 result[4..8] = ByteConverter.BigEndian.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:
6
diff
changeset
|
168 result[8..12] = ByteConverter.BigEndian.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:
6
diff
changeset
|
169 result[12..16] = ByteConverter.BigEndian.from!(uint)(h3); |
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:
6
diff
changeset
|
170 result[16..20] = ByteConverter.BigEndian.from!(uint)(h4); |
0 | 171 |
172 reset(); | |
173 return result; | |
174 } | |
175 | |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
176 void reset() |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
177 { |
0 | 178 super.reset(); |
179 h0 = 0x67452301u; | |
180 h1 = 0xefcdab89u; | |
181 h2 = 0x98badcfeu; | |
182 h3 = 0x10325476u; | |
183 h4 = 0xc3d2e1f0u; | |
184 } | |
185 | |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
186 SHA1 copy() |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
187 { |
2
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
188 SHA1 h = new SHA1(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
|
189 h.bytes = bytes; |
2
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
190 h.h0 = h0; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
191 h.h1 = h1; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
192 h.h2 = h2; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
193 h.h3 = h3; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
194 h.h4 = h4; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
195 return h; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
196 } |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
197 |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
198 debug (UnitTest) |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
199 { |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
200 unittest |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
201 { |
28
ad687db713a4
Further reworked the code for hash padding. Replaced all instances of 'char[]' with 'string' and removed a few 'const' modifiers as per Glenn Haecker's patch for D2 compatibility. Updated CONTRIBUTORS file.
Thomas Dixon <reikon@reikon.us>
parents:
27
diff
changeset
|
202 static string[] test_inputs = [ |
0 | 203 "", |
204 "abc", | |
205 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", | |
206 "a", | |
207 "0123456701234567012345670123456701234567012345670123456701234567" | |
208 ]; | |
209 | |
28
ad687db713a4
Further reworked the code for hash padding. Replaced all instances of 'char[]' with 'string' and removed a few 'const' modifiers as per Glenn Haecker's patch for D2 compatibility. Updated CONTRIBUTORS file.
Thomas Dixon <reikon@reikon.us>
parents:
27
diff
changeset
|
210 static int[] test_repeat = [ |
0 | 211 1, 1, 1, 1000000, 10 |
212 ]; | |
213 | |
28
ad687db713a4
Further reworked the code for hash padding. Replaced all instances of 'char[]' with 'string' and removed a few 'const' modifiers as per Glenn Haecker's patch for D2 compatibility. Updated CONTRIBUTORS file.
Thomas Dixon <reikon@reikon.us>
parents:
27
diff
changeset
|
214 static string[] test_results = [ |
0 | 215 "da39a3ee5e6b4b0d3255bfef95601890afd80709", |
216 "a9993e364706816aba3e25717850c26c9cd0d89d", | |
217 "84983e441c3bd26ebaae4aa1f95129e5e54670f1", | |
218 "34aa973cd4c4daa4f61eeb2bdbad27316534016f", | |
219 "dea356a2cddd90c7a7ecedc5ebb563934f460452" | |
220 ]; | |
221 | |
222 SHA1 h = new SHA1(); | |
28
ad687db713a4
Further reworked the code for hash padding. Replaced all instances of 'char[]' with 'string' and removed a few 'const' modifiers as per Glenn Haecker's patch for D2 compatibility. Updated CONTRIBUTORS file.
Thomas Dixon <reikon@reikon.us>
parents:
27
diff
changeset
|
223 foreach (uint i, string input; test_inputs) |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
224 { |
0 | 225 for (int j = 0; j < test_repeat[i]; j++) |
226 h.update(input); | |
28
ad687db713a4
Further reworked the code for hash padding. Replaced all instances of 'char[]' with 'string' and removed a few 'const' modifiers as per Glenn Haecker's patch for D2 compatibility. Updated CONTRIBUTORS file.
Thomas Dixon <reikon@reikon.us>
parents:
27
diff
changeset
|
227 string digest = h.hexDigest(); |
0 | 228 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
|
229 h.name~": ("~digest~") != ("~test_results[i]~")"); |
0 | 230 } |
231 } | |
232 } | |
233 } |