Mercurial > projects > dcrypt
annotate dcrypt/crypto/Hash.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 | cff9960a019c |
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 { | |
15 private const enum { | |
16 MODE_MD=0, // MDx, RipeMD, etc | |
17 MODE_SHA, | |
18 MODE_TIGER | |
19 } | |
2
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
20 |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
21 protected { |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
22 ubyte[] buffer; |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
23 uint bytes, |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
24 index; |
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 |
0 | 27 this (void[] input_=null) { |
28 buffer = new ubyte[blockSize]; | |
29 ubyte[] input = cast(ubyte[]) input_; | |
30 if (input) | |
31 update(input); | |
32 } | |
2
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
33 |
0 | 34 /** Returns: The block size of the hash function in bytes. */ |
35 abstract uint blockSize(); | |
36 | |
37 /** Returns: The output size of the hash function in bytes. */ | |
38 abstract uint digestSize(); | |
39 | |
40 /** Returns: The name of the algorithm we're implementing. */ | |
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 |
0 | 46 /** |
47 * Introduce data into the hash function. | |
48 * | |
49 * Params: | |
50 * input_ = Data to be processed. | |
51 */ | |
52 void update(void[] input_) { | |
53 ubyte[] input = cast(ubyte[]) input_; | |
54 foreach (ubyte i; input) | |
55 update(i); | |
56 } | |
57 | |
58 void update(ubyte input) { | |
59 bytes++; | |
60 buffer[index++] = input; | |
61 if (index == blockSize) { | |
62 transform(buffer); | |
63 index = 0; | |
64 } | |
65 } | |
66 | |
67 /** Hash function's internal transformation. */ | |
68 protected abstract void transform(ubyte[] input); | |
69 | |
70 /** | |
71 * Pad message in the respective manner. | |
72 * | |
73 * Params: | |
74 * mode = Mode constant dictating in which manner | |
75 * to pad the message. | |
76 */ | |
77 protected void padMessage(uint mode) { | |
78 ulong length = bytes << 3; | |
79 | |
80 update(((mode == MODE_TIGER) ? 0x01 : 0x80)); | |
81 uint count = (blockSize-(blockSize >> 3)); | |
82 if (index > count) | |
83 while (index != 0) | |
84 update(0); | |
85 while (index < (count + ((blockSize == 128) ? 8 : 0))) | |
86 update(0); | |
87 if (mode == MODE_SHA) | |
88 for (int i = 56; i >= 0; i-=8) // big endian | |
89 update(length >> i); | |
90 else | |
91 for (int i = 0; i < 64; i+=8) // little endian | |
92 update(length >> i); | |
93 } | |
94 | |
95 /** | |
96 * Process all data, pad and finalize. This method will | |
97 * reset the digest to its original state ofr subsequent use. | |
98 * | |
99 * Returns: Binary representation of the hash in bytes. | |
100 */ | |
101 abstract ubyte[] digest(); | |
102 | |
103 /** | |
104 * Same as digest() but returns hash value in hex. | |
105 * | |
106 * Returns: Representation of the final hash value in hex. | |
107 */ | |
108 char[] hexDigest() { | |
109 return Util.ubytesToHex(digest()); | |
110 } | |
111 | |
112 /** Reset hash to initial state. */ | |
113 void reset() { | |
114 bytes = index = 0; | |
115 } | |
116 } |