Mercurial > projects > dcrypt
annotate dcrypt/crypto/Hash.d @ 12:8c7f8fecdd75
Added ManagedBlockCipher, changed Crypto to just import everything, made Hash.update() return itself (for chaining) and ditched BlockCipherWrapper.
author | Thomas Dixon <reikon@reikon.us> |
---|---|
date | Sat, 30 Aug 2008 14:38:23 -0400 |
parents | cff9960a019c |
children | 5ce3012f1def |
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.Hash; | |
10 | |
11 public import dcrypt.misc.Util; | |
12 | |
13 /** Base class for all cryptographic hash functions */ | |
14 class Hash { | |
5
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
15 private const enum { |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
16 MODE_MD=0, // MDx, RipeMD, etc |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
17 MODE_SHA, |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
18 MODE_TIGER |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
19 } |
2
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
20 |
5
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
21 protected { |
2
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
22 ubyte[] buffer; |
5
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
23 ulong bytes; |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
24 uint index; |
2
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
25 } |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
26 |
5
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
27 this (void[] input_=null) { |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
28 buffer = new ubyte[blockSize]; |
0 | 29 ubyte[] input = cast(ubyte[]) input_; |
30 if (input) | |
31 update(input); | |
5
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
32 } |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
33 |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
34 /** Returns: The block size of the hash function in bytes. */ |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
35 abstract uint blockSize(); |
2
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
36 |
5
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
37 /** Returns: The output size of the hash function in bytes. */ |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
38 abstract uint digestSize(); |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
39 |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
40 /** Returns: The name of the algorithm we're implementing. */ |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
41 abstract char[] name(); |
2
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
42 |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
43 /** Returns: A copy of this hash object. */ |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
44 abstract Hash copy(); |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
45 |
5
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
46 /** |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
47 * Introduce data into the hash function. |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
48 * |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
49 * Params: |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
50 * input_ = Data to be processed. |
12
8c7f8fecdd75
Added ManagedBlockCipher, changed Crypto to just import everything, made Hash.update() return itself (for chaining) and ditched BlockCipherWrapper.
Thomas Dixon <reikon@reikon.us>
parents:
5
diff
changeset
|
51 * |
8c7f8fecdd75
Added ManagedBlockCipher, changed Crypto to just import everything, made Hash.update() return itself (for chaining) and ditched BlockCipherWrapper.
Thomas Dixon <reikon@reikon.us>
parents:
5
diff
changeset
|
52 * Returns: Self |
5
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
53 */ |
12
8c7f8fecdd75
Added ManagedBlockCipher, changed Crypto to just import everything, made Hash.update() return itself (for chaining) and ditched BlockCipherWrapper.
Thomas Dixon <reikon@reikon.us>
parents:
5
diff
changeset
|
54 Hash update(void[] input_) { |
5
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
55 ubyte[] input = cast(ubyte[]) input_; |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
56 foreach (ubyte i; input) { |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
57 bytes++; |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
58 buffer[index++] = i; |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
59 if (index == blockSize) { |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
60 transform(buffer); |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
61 index = 0; |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
62 } |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
63 } |
12
8c7f8fecdd75
Added ManagedBlockCipher, changed Crypto to just import everything, made Hash.update() return itself (for chaining) and ditched BlockCipherWrapper.
Thomas Dixon <reikon@reikon.us>
parents:
5
diff
changeset
|
64 return this; |
5
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
65 } |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
66 |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
67 /** Hash function's internal transformation. */ |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
68 protected abstract void transform(ubyte[] input); |
0 | 69 |
5
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
70 /** |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
71 * Pad message in the respective manner. |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
72 * |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
73 * Params: |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
74 * mode = Mode constant dictating in which manner |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
75 * to pad the message. |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
76 */ |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
77 protected void padMessage(uint mode) { |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
78 ulong bits = bytes << 3; |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
79 |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
80 // Add the pad marker |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
81 buffer[index++] = ((mode == MODE_TIGER) ? 0x01 : 0x80); |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
82 if (index == blockSize) { |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
83 transform(buffer); |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
84 index = 0; |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
85 } |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
86 |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
87 // Pad with null bytes |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
88 if (index >= (blockSize-(blockSize >> 3))) |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
89 update(new ubyte[blockSize-index]); |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
90 update(new ubyte[(blockSize-ulong.sizeof)-index]); |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
91 |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
92 // Length padding |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
93 ubyte[] length = new ubyte[ulong.sizeof]; |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
94 for (int i = 0, j = 0; i < 64; i+=8) // little endian |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
95 length[j++] = bits >> i; |
0 | 96 if (mode == MODE_SHA) |
5
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
97 length.reverse; // big endian |
cff9960a019c
Removed update(ubyte x) from Hash class. Rewrote hash function padding. Updated hash functions to use ulong counter for bytes (was previously uint).
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
98 update(length); |
0 | 99 } |
100 | |
101 /** | |
102 * Process all data, pad and finalize. This method will | |
103 * reset the digest to its original state ofr subsequent use. | |
104 * | |
105 * Returns: Binary representation of the hash in bytes. | |
106 */ | |
107 abstract ubyte[] digest(); | |
108 | |
109 /** | |
110 * Same as digest() but returns hash value in hex. | |
111 * | |
112 * Returns: Representation of the final hash value in hex. | |
113 */ | |
114 char[] hexDigest() { | |
115 return Util.ubytesToHex(digest()); | |
116 } | |
117 | |
118 /** Reset hash to initial state. */ | |
119 void reset() { | |
120 bytes = index = 0; | |
121 } | |
122 } |