Mercurial > projects > dcrypt
annotate dcrypt/crypto/Hash.d @ 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).
author | Thomas Dixon <reikon@reikon.us> |
---|---|
date | Thu, 14 Aug 2008 23:51:56 -0400 |
parents | 71aae178f89a |
children | 8c7f8fecdd75 |
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. |
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
|
51 */ |
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
|
52 void update(void[] 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
|
53 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
|
54 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
|
55 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
|
56 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
|
57 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
|
58 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
|
59 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
|
60 } |
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 } |
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 } |
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
|
64 |
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 /** 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
|
66 protected abstract void transform(ubyte[] input); |
0 | 67 |
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
|
68 /** |
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
|
69 * 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
|
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 * 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
|
72 * 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
|
73 * 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
|
74 */ |
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 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
|
76 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
|
77 |
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 // 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
|
79 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
|
80 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
|
81 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
|
82 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
|
83 } |
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 |
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 // 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
|
86 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
|
87 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
|
88 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
|
89 |
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 // 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
|
91 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
|
92 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
|
93 length[j++] = bits >> i; |
0 | 94 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
|
95 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
|
96 update(length); |
0 | 97 } |
98 | |
99 /** | |
100 * Process all data, pad and finalize. This method will | |
101 * reset the digest to its original state ofr subsequent use. | |
102 * | |
103 * Returns: Binary representation of the hash in bytes. | |
104 */ | |
105 abstract ubyte[] digest(); | |
106 | |
107 /** | |
108 * Same as digest() but returns hash value in hex. | |
109 * | |
110 * Returns: Representation of the final hash value in hex. | |
111 */ | |
112 char[] hexDigest() { | |
113 return Util.ubytesToHex(digest()); | |
114 } | |
115 | |
116 /** Reset hash to initial state. */ | |
117 void reset() { | |
118 bytes = index = 0; | |
119 } | |
120 } |