comparison dcrypt/crypto/hashes/SHA256.d @ 0:0e08791a1418

Initial import.
author Thomas Dixon <reikon@reikon.us>
date Sun, 10 Aug 2008 14:20:17 -0400
parents
children 71aae178f89a
comparison
equal deleted inserted replaced
-1:000000000000 0:0e08791a1418
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.SHA256;
10
11 public import dcrypt.crypto.Hash;
12
13 /** Implementation of SHA-256. */
14 class SHA256 : Hash {
15 private const uint[] K = [
16 0x428a2f98u, 0x71374491u, 0xb5c0fbcfu, 0xe9b5dba5u,
17 0x3956c25bu, 0x59f111f1u, 0x923f82a4u, 0xab1c5ed5u,
18 0xd807aa98u, 0x12835b01u, 0x243185beu, 0x550c7dc3u,
19 0x72be5d74u, 0x80deb1feu, 0x9bdc06a7u, 0xc19bf174u,
20 0xe49b69c1u, 0xefbe4786u, 0x0fc19dc6u, 0x240ca1ccu,
21 0x2de92c6fu, 0x4a7484aau, 0x5cb0a9dcu, 0x76f988dau,
22 0x983e5152u, 0xa831c66du, 0xb00327c8u, 0xbf597fc7u,
23 0xc6e00bf3u, 0xd5a79147u, 0x06ca6351u, 0x14292967u,
24 0x27b70a85u, 0x2e1b2138u, 0x4d2c6dfcu, 0x53380d13u,
25 0x650a7354u, 0x766a0abbu, 0x81c2c92eu, 0x92722c85u,
26 0xa2bfe8a1u, 0xa81a664bu, 0xc24b8b70u, 0xc76c51a3u,
27 0xd192e819u, 0xd6990624u, 0xf40e3585u, 0x106aa070u,
28 0x19a4c116u, 0x1e376c08u, 0x2748774cu, 0x34b0bcb5u,
29 0x391c0cb3u, 0x4ed8aa4au, 0x5b9cca4fu, 0x682e6ff3u,
30 0x748f82eeu, 0x78a5636fu, 0x84c87814u, 0x8cc70208u,
31 0x90befffau, 0xa4506cebu, 0xbef9a3f7u, 0xc67178f2u
32 ];
33 protected uint h0, h1, h2, h3, h4, h5, h6, h7;
34
35 this (void[] input_=null) {
36 reset();
37 super(input_);
38 }
39
40 uint blockSize() {
41 return 64;
42 }
43
44 uint digestSize() {
45 return 32;
46 }
47
48 char[] name() {
49 return "SHA256";
50 }
51
52 void transform(ubyte[] input) {
53 uint[] w = new uint[64];
54
55 for (int i = 0, j = 0; i < 16; i++,j+=4)
56 w[i] = Util.ubytesToUintBig(input, j);
57
58 for (int i = 16; i < 64; i++)
59 w[i] = theta1(w[i-2]) + w[i-7] + theta0(w[i-15]) + w[i-16];
60
61 uint a = h0,
62 b = h1,
63 c = h2,
64 d = h3,
65 e = h4,
66 f = h5,
67 g = h6,
68 h = h7;
69
70 for (int i = 0; i < 64; i++) {
71 uint t1 = h + sum1(e) + ch(e,f,g) + K[i] + w[i],
72 t2 = sum0(a) + maj(a,b,c);
73 h = g;
74 g = f;
75 f = e;
76 e = d + t1;
77 d = c;
78 c = b;
79 b = a;
80 a = t1 + t2;
81 }
82
83 h0 += a;
84 h1 += b;
85 h2 += c;
86 h3 += d;
87 h4 += e;
88 h5 += f;
89 h6 += g;
90 h7 += h;
91 }
92
93 private uint ch(uint x, uint y, uint z) {
94 return (x&y)^(~x&z);
95 }
96
97 private uint maj(uint x, uint y, uint z) {
98 return (x&y)^(x&z)^(y&z);
99 }
100
101 private uint sum0(uint x) {
102 return Util.rotateRight(x,2)^Util.rotateRight(x,13)^Util.rotateRight(x,22);
103 }
104
105 private uint sum1(uint x) {
106 return Util.rotateRight(x,6)^Util.rotateRight(x,11)^Util.rotateRight(x,25);
107 }
108
109 private uint theta0(uint x) {
110 return Util.rotateRight(x,7)^Util.rotateRight(x,18)^(x >> 3);
111 }
112
113 private uint theta1(uint x) {
114 return Util.rotateRight(x,17)^Util.rotateRight(x,19)^(x >> 10);
115 }
116
117 ubyte[] digest() {
118 padMessage(MODE_SHA);
119 ubyte[] result = new ubyte[digestSize];
120
121 Util.uintToUbytesBig(h0, result, 0);
122 Util.uintToUbytesBig(h1, result, 4);
123 Util.uintToUbytesBig(h2, result, 8);
124 Util.uintToUbytesBig(h3, result, 12);
125 Util.uintToUbytesBig(h4, result, 16);
126 Util.uintToUbytesBig(h5, result, 20);
127 Util.uintToUbytesBig(h6, result, 24);
128 Util.uintToUbytesBig(h7, result, 28);
129
130 reset();
131 return result;
132 }
133
134 void reset() {
135 super.reset();
136 h0 = 0x6a09e667u;
137 h1 = 0xbb67ae85u;
138 h2 = 0x3c6ef372u;
139 h3 = 0xa54ff53au;
140 h4 = 0x510e527fu;
141 h5 = 0x9b05688cu;
142 h6 = 0x1f83d9abu;
143 h7 = 0x5be0cd19u;
144 }
145
146 version (UnitTest) {
147 unittest {
148 static const char[][] test_inputs = [
149 "",
150 "abc",
151 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
152 "a"
153 ];
154
155 static const int[] test_repeat = [
156 1, 1, 1, 1000000
157 ];
158
159 static const char[][] test_results = [
160 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
161 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
162 "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1",
163 "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0"
164 ];
165
166 SHA256 h = new SHA256();
167 foreach (uint i, char[] input; test_inputs) {
168 for (int j = 0; j < test_repeat[i]; j++)
169 h.update(input);
170 char[] digest = h.hexDigest();
171 assert(digest == test_results[i],
172 h.name()~": ("~digest~") != ("~test_results[i]~")");
173 }
174 }
175 }
176 }