Mercurial > projects > dcrypt
annotate dcrypt/crypto/hashes/SHA224.d @ 6:5cb17e09d685
Minor edits to the unittests of hash functions and ciphers. Added AES and test vectors.
author | Thomas Dixon <reikon@reikon.us> |
---|---|
date | Sat, 16 Aug 2008 22:43:22 -0400 |
parents | a5789a7b3b3b |
children | 4589f8c5eb3c |
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.SHA224; | |
10 | |
11 import dcrypt.crypto.hashes.SHA256; | |
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-224. |
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_changenotice.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 */ |
0 | 19 class SHA224 : SHA256 { |
20 this (void[] input_=null) { | |
21 reset(); | |
22 super(input_); | |
23 } | |
24 | |
25 uint digestSize() { | |
26 return 28; | |
27 } | |
28 | |
29 char[] name() { | |
30 return "SHA224"; | |
31 } | |
32 | |
33 ubyte[] digest() { | |
34 padMessage(MODE_SHA); | |
35 | |
36 ubyte[] result = new ubyte[digestSize]; | |
37 | |
38 Util.uintToUbytesBig(h0, result, 0); | |
39 Util.uintToUbytesBig(h1, result, 4); | |
40 Util.uintToUbytesBig(h2, result, 8); | |
41 Util.uintToUbytesBig(h3, result, 12); | |
42 Util.uintToUbytesBig(h4, result, 16); | |
43 Util.uintToUbytesBig(h5, result, 20); | |
44 Util.uintToUbytesBig(h6, result, 24); | |
2
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
45 |
0 | 46 reset(); |
47 return result; | |
48 } | |
49 | |
50 void reset() { | |
51 super.reset(); | |
52 h0 = 0xc1059ed8u; | |
53 h1 = 0x367cd507u; | |
54 h2 = 0x3070dd17u; | |
55 h3 = 0xf70e5939u; | |
56 h4 = 0xffc00b31u; | |
57 h5 = 0x68581511u; | |
58 h6 = 0x64f98fa7u; | |
59 h7 = 0xbefa4fa4u; | |
60 } | |
61 | |
2
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
62 SHA224 copy() { |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
63 SHA224 h = new SHA224(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
|
64 h.bytes = bytes; |
2
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
65 h.h0 = h0; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
66 h.h1 = h1; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
67 h.h2 = h2; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
68 h.h3 = h3; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
69 h.h4 = h4; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
70 h.h5 = h5; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
71 h.h6 = h6; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
72 h.h7 = h7; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
73 return h; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
74 } |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
75 |
0 | 76 version (UnitTest) { |
77 unittest { | |
78 static const char[][] test_inputs = [ | |
79 "", | |
80 "abc", | |
81 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", | |
82 "a" | |
83 ]; | |
84 | |
85 static const int[] test_repeat = [ | |
86 1, 1, 1, 1000000 | |
87 ]; | |
88 | |
89 static const char[][] test_results = [ | |
90 "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", | |
91 "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7", | |
92 "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525", | |
93 "20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67" | |
94 ]; | |
95 | |
96 SHA224 h = new SHA224(); | |
97 foreach (uint i, char[] input; test_inputs) { | |
98 for (int j = 0; j < test_repeat[i]; j++) | |
99 h.update(input); | |
100 char[] digest = h.hexDigest(); | |
101 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
|
102 h.name~": ("~digest~") != ("~test_results[i]~")"); |
0 | 103 } |
104 } | |
105 } | |
106 } |