annotate dcrypt/crypto/hashes/SHA256.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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
1 /**
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
2 * This file is part of the dcrypt project.
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
3 *
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
4 * Copyright: Copyright (C) dcrypt contributors 2008. All rights reserved.
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
5 * License: MIT
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
6 * Authors: Thomas Dixon
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
7 */
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
8
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
9 module dcrypt.crypto.hashes.SHA256;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
10
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
11 public import dcrypt.crypto.Hash;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
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-256.
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-2
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://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
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 */
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
19 class SHA256 : Hash
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
20 {
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
21 private static const uint[] K = [
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
22 0x428a2f98u, 0x71374491u, 0xb5c0fbcfu, 0xe9b5dba5u,
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
23 0x3956c25bu, 0x59f111f1u, 0x923f82a4u, 0xab1c5ed5u,
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
24 0xd807aa98u, 0x12835b01u, 0x243185beu, 0x550c7dc3u,
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
25 0x72be5d74u, 0x80deb1feu, 0x9bdc06a7u, 0xc19bf174u,
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
26 0xe49b69c1u, 0xefbe4786u, 0x0fc19dc6u, 0x240ca1ccu,
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
27 0x2de92c6fu, 0x4a7484aau, 0x5cb0a9dcu, 0x76f988dau,
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
28 0x983e5152u, 0xa831c66du, 0xb00327c8u, 0xbf597fc7u,
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
29 0xc6e00bf3u, 0xd5a79147u, 0x06ca6351u, 0x14292967u,
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
30 0x27b70a85u, 0x2e1b2138u, 0x4d2c6dfcu, 0x53380d13u,
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
31 0x650a7354u, 0x766a0abbu, 0x81c2c92eu, 0x92722c85u,
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
32 0xa2bfe8a1u, 0xa81a664bu, 0xc24b8b70u, 0xc76c51a3u,
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
33 0xd192e819u, 0xd6990624u, 0xf40e3585u, 0x106aa070u,
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
34 0x19a4c116u, 0x1e376c08u, 0x2748774cu, 0x34b0bcb5u,
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
35 0x391c0cb3u, 0x4ed8aa4au, 0x5b9cca4fu, 0x682e6ff3u,
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
36 0x748f82eeu, 0x78a5636fu, 0x84c87814u, 0x8cc70208u,
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
37 0x90befffau, 0xa4506cebu, 0xbef9a3f7u, 0xc67178f2u
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
38 ];
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
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 protected uint h0, h1, h2, h3, h4, h5, h6, h7;
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
41
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
42 this (void[] input_=null)
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
43 {
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
44 reset();
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
45 super(input_);
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
46 }
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
47
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
48 uint blockSize()
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
49 {
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
50 return 64;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
51 }
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
52
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
53 uint digestSize()
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
54 {
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
55 return 32;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
56 }
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
57
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
58 string name()
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
59 {
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
60 return "SHA256";
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
61 }
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
62
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
63 void transform(ubyte[] input)
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
64 {
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
65 uint[] w = new uint[64];
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
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 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
68 w[i] = ByteConverter.BigEndian.to!(uint)(input[j..j+int.sizeof]);
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
69
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
70 for (int i = 16; i < 64; i++)
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
71 w[i] = theta1(w[i-2]) + w[i-7] + theta0(w[i-15]) + w[i-16];
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
72
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
73 uint a = h0,
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
74 b = h1,
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
75 c = h2,
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
76 d = h3,
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
77 e = h4,
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
78 f = h5,
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
79 g = h6,
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
80 h = h7;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
81
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
82 for (int i = 0; i < 64; i++)
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
83 {
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
84 uint t1 = h + sum1(e) + ch(e,f,g) + K[i] + w[i],
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
85 t2 = sum0(a) + maj(a,b,c);
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
86 h = g;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
87 g = f;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
88 f = e;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
89 e = d + t1;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
90 d = c;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
91 c = b;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
92 b = a;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
93 a = t1 + t2;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
94 }
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
95
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
96 h0 += a;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
97 h1 += b;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
98 h2 += c;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
99 h3 += d;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
100 h4 += e;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
101 h5 += f;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
102 h6 += g;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
103 h7 += h;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
104 }
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
105
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
106 private uint ch(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
107 {
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
108 return (z ^ (x & (y ^ z)));
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
109 }
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
110
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
111 private uint maj(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
112 {
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
113 return ((x & y) | (z & (x ^ y)));
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
114 }
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
115
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
116 private uint sum0(uint x)
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 return Bitwise.rotateRight(x,2)^Bitwise.rotateRight(x,13)^Bitwise.rotateRight(x,22);
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
119 }
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
120
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
121 private uint sum1(uint x)
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
122 {
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
123 return Bitwise.rotateRight(x,6)^Bitwise.rotateRight(x,11)^Bitwise.rotateRight(x,25);
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
124 }
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
125
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
126 private uint theta0(uint x)
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
127 {
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
128 return Bitwise.rotateRight(x,7)^Bitwise.rotateRight(x,18)^(x >> 3);
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
129 }
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
130
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
131 private uint theta1(uint x)
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
132 {
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
133 return Bitwise.rotateRight(x,17)^Bitwise.rotateRight(x,19)^(x >> 10);
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
134 }
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
135
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
136 ubyte[] digest()
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
137 {
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
138 padMessage(MODE_SHA);
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
139 ubyte[] result = new ubyte[digestSize];
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
140
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
141 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
142 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
143 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
144 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
145 result[16..20] = ByteConverter.BigEndian.from!(uint)(h4);
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
146 result[20..24] = ByteConverter.BigEndian.from!(uint)(h5);
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
147 result[24..28] = ByteConverter.BigEndian.from!(uint)(h6);
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
148 result[28..32] = ByteConverter.BigEndian.from!(uint)(h7);
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
149
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
150 reset();
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
151 return result;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
152 }
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
153
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
154 void reset()
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
155 {
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
156 super.reset();
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
157 h0 = 0x6a09e667u;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
158 h1 = 0xbb67ae85u;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
159 h2 = 0x3c6ef372u;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
160 h3 = 0xa54ff53au;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
161 h4 = 0x510e527fu;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
162 h5 = 0x9b05688cu;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
163 h6 = 0x1f83d9abu;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
164 h7 = 0x5be0cd19u;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
165 }
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
166
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
167 SHA256 copy()
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
168 {
2
71aae178f89a Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents: 0
diff changeset
169 SHA256 h = new SHA256(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
170 h.bytes = bytes;
2
71aae178f89a Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents: 0
diff changeset
171 h.h0 = h0;
71aae178f89a Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents: 0
diff changeset
172 h.h1 = h1;
71aae178f89a Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents: 0
diff changeset
173 h.h2 = h2;
71aae178f89a Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents: 0
diff changeset
174 h.h3 = h3;
71aae178f89a Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents: 0
diff changeset
175 h.h4 = h4;
71aae178f89a Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents: 0
diff changeset
176 h.h5 = h5;
71aae178f89a Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents: 0
diff changeset
177 h.h6 = h6;
71aae178f89a Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents: 0
diff changeset
178 h.h7 = h7;
71aae178f89a Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents: 0
diff changeset
179 return h;
71aae178f89a Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents: 0
diff changeset
180 }
71aae178f89a Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents: 0
diff changeset
181
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
182 debug (UnitTest)
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
183 {
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
184 unittest
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
185 {
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
186 static string[] test_inputs = [
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
187 "",
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
188 "abc",
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
189 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
190 "a"
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
191 ];
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
192
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
193 static int[] test_repeat = [
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
194 1, 1, 1, 1000000
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
195 ];
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
196
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
197 static string[] test_results = [
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
198 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
199 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
200 "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1",
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
201 "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0"
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
202 ];
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
203
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
204 SHA256 h = new SHA256();
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
205 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
206 {
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
207 for (int j = 0; j < test_repeat[i]; j++)
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
208 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
209 string digest = h.hexDigest();
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
210 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
211 h.name~": ("~digest~") != ("~test_results[i]~")");
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
212 }
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
213 }
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
214 }
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
215 }