Mercurial > projects > dcrypt
annotate dcrypt/crypto/hashes/SHA384.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.SHA384; | |
10 | |
11 import dcrypt.crypto.hashes.SHA512; | |
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-384. |
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 */ |
0 | 19 class SHA384 : SHA512 { |
20 this (void[] input_=null) { | |
21 reset(); | |
22 super(input_); | |
23 } | |
24 | |
25 uint digestSize() { | |
26 return 48; | |
27 } | |
28 | |
29 char[] name() { | |
30 return "SHA384"; | |
31 } | |
32 | |
33 ubyte[] digest() { | |
34 padMessage(MODE_SHA); | |
35 ubyte[] result = new ubyte[digestSize]; | |
36 | |
37 Util.ulongToUbytesBig(h0, result, 0); | |
38 Util.ulongToUbytesBig(h1, result, 8); | |
39 Util.ulongToUbytesBig(h2, result, 16); | |
40 Util.ulongToUbytesBig(h3, result, 24); | |
41 Util.ulongToUbytesBig(h4, result, 32); | |
42 Util.ulongToUbytesBig(h5, result, 40); | |
43 | |
44 reset(); | |
45 return result; | |
46 } | |
47 | |
48 void reset() { | |
49 super.reset(); | |
50 h0 = 0xcbbb9d5dc1059ed8u, | |
51 h1 = 0x629a292a367cd507u, | |
52 h2 = 0x9159015a3070dd17u, | |
53 h3 = 0x152fecd8f70e5939u, | |
54 h4 = 0x67332667ffc00b31u, | |
55 h5 = 0x8eb44a8768581511u, | |
56 h6 = 0xdb0c2e0d64f98fa7u, | |
57 h7 = 0x47b5481dbefa4fa4u; | |
58 } | |
59 | |
2
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
60 SHA384 copy() { |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
61 SHA384 h = new SHA384(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
|
62 h.bytes = bytes; |
2
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
63 h.h0 = h0; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
64 h.h1 = h1; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
65 h.h2 = h2; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
66 h.h3 = h3; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
67 h.h4 = h4; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
68 h.h5 = h5; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
69 h.h6 = h6; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
70 h.h7 = h7; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
71 return h; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
72 } |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
73 |
0 | 74 version (UnitTest) { |
75 unittest { | |
76 static const char[][] test_inputs = [ | |
77 "", | |
78 "abc", | |
79 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"~ | |
80 "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", | |
81 "a" | |
82 ]; | |
83 | |
84 static const int[] test_repeat = [ | |
85 1, 1, 1, 1000000 | |
86 ]; | |
87 | |
88 static const char[][] test_results = [ | |
89 "38b060a751ac96384cd9327eb1b1e36a21fdb71114be0743"~ | |
90 "4c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b", | |
91 | |
92 "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded163"~ | |
93 "1a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7", | |
94 | |
95 "09330c33f71147e83d192fc782cd1b4753111b173b3b05d2"~ | |
96 "2fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039", | |
97 | |
98 "9d0e1809716474cb086e834e310a4a1ced149e9c00f24852"~ | |
99 "7972cec5704c2a5b07b8b3dc38ecc4ebae97ddd87f3d8985" | |
100 ]; | |
101 | |
102 SHA384 h = new SHA384(); | |
103 foreach (uint i, char[] input; test_inputs) { | |
104 for (int j = 0; j < test_repeat[i]; j++) | |
105 h.update(input); | |
106 char[] digest = h.hexDigest(); | |
107 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
|
108 h.name~": ("~digest~") != ("~test_results[i]~")"); |
0 | 109 } |
110 } | |
111 } | |
112 } |