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