Mercurial > projects > dcrypt
annotate dcrypt/crypto/hashes/SHA384.d @ 2:71aae178f89a
Added copy() to hash functions. Modified some code style.
author | Thomas Dixon <reikon@reikon.us> |
---|---|
date | Wed, 13 Aug 2008 22:01:19 -0400 |
parents | 0e08791a1418 |
children | a5789a7b3b3b |
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 | |
13 /** Implementation of SHA-384. */ | |
14 class SHA384 : SHA512 { | |
15 this (void[] input_=null) { | |
16 reset(); | |
17 super(input_); | |
18 } | |
19 | |
20 uint digestSize() { | |
21 return 48; | |
22 } | |
23 | |
24 char[] name() { | |
25 return "SHA384"; | |
26 } | |
27 | |
28 ubyte[] digest() { | |
29 padMessage(MODE_SHA); | |
30 ubyte[] result = new ubyte[digestSize]; | |
31 | |
32 Util.ulongToUbytesBig(h0, result, 0); | |
33 Util.ulongToUbytesBig(h1, result, 8); | |
34 Util.ulongToUbytesBig(h2, result, 16); | |
35 Util.ulongToUbytesBig(h3, result, 24); | |
36 Util.ulongToUbytesBig(h4, result, 32); | |
37 Util.ulongToUbytesBig(h5, result, 40); | |
38 | |
39 reset(); | |
40 return result; | |
41 } | |
42 | |
43 void reset() { | |
44 super.reset(); | |
45 h0 = 0xcbbb9d5dc1059ed8u, | |
46 h1 = 0x629a292a367cd507u, | |
47 h2 = 0x9159015a3070dd17u, | |
48 h3 = 0x152fecd8f70e5939u, | |
49 h4 = 0x67332667ffc00b31u, | |
50 h5 = 0x8eb44a8768581511u, | |
51 h6 = 0xdb0c2e0d64f98fa7u, | |
52 h7 = 0x47b5481dbefa4fa4u; | |
53 } | |
54 | |
2
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
55 SHA384 copy() { |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
56 SHA384 h = new SHA384(buffer[0..index]); |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
57 h.h0 = h0; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
58 h.h1 = h1; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
59 h.h2 = h2; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
60 h.h3 = h3; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
61 h.h4 = h4; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
62 h.h5 = h5; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
63 h.h6 = h6; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
64 h.h7 = h7; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
65 return h; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
66 } |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
67 |
0 | 68 version (UnitTest) { |
69 unittest { | |
70 static const char[][] test_inputs = [ | |
71 "", | |
72 "abc", | |
73 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"~ | |
74 "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", | |
75 "a" | |
76 ]; | |
77 | |
78 static const int[] test_repeat = [ | |
79 1, 1, 1, 1000000 | |
80 ]; | |
81 | |
82 static const char[][] test_results = [ | |
83 "38b060a751ac96384cd9327eb1b1e36a21fdb71114be0743"~ | |
84 "4c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b", | |
85 | |
86 "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded163"~ | |
87 "1a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7", | |
88 | |
89 "09330c33f71147e83d192fc782cd1b4753111b173b3b05d2"~ | |
90 "2fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039", | |
91 | |
92 "9d0e1809716474cb086e834e310a4a1ced149e9c00f24852"~ | |
93 "7972cec5704c2a5b07b8b3dc38ecc4ebae97ddd87f3d8985" | |
94 ]; | |
95 | |
96 SHA384 h = new SHA384(); | |
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 } |