Mercurial > projects > dcrypt
annotate dcrypt/crypto/Hash.d @ 33:b9f8aa42a547
More changes suggested by Glenn Haecker for D2 compatibility.
author | Thomas Dixon <reikon@reikon.us> |
---|---|
date | Thu, 14 May 2009 01:33:11 -0400 |
parents | 2b4bccdc8387 |
children |
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 | |
23
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
14
diff
changeset
|
11 public import dcrypt.misc.ByteConverter; |
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
14
diff
changeset
|
12 public import dcrypt.misc.Bitwise; |
0 | 13 |
14 /** Base class for all cryptographic hash functions */ | |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
15 class Hash |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
16 { |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
17 private const enum |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
18 { |
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
|
19 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
|
20 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
|
21 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
|
22 } |
2
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
23 |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
24 protected |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
25 { |
2
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
26 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
|
27 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
|
28 uint index; |
2
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
29 } |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
30 |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
31 this (void[] input_=null) |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
32 { |
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
|
33 buffer = new ubyte[blockSize]; |
0 | 34 ubyte[] input = cast(ubyte[]) input_; |
35 if (input) | |
36 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
|
37 } |
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 |
32
2b4bccdc8387
Added version() statements to play nice with D2's current feelings about const. Changed a few methods (addEntropy and read in the base PRNG class, and the constructor for ParametersWithIV) to accept void[] in place of ubyte[].
Thomas Dixon <reikon@reikon.us>
parents:
30
diff
changeset
|
39 /** Play nice with D2's idea of const. */ |
2b4bccdc8387
Added version() statements to play nice with D2's current feelings about const. Changed a few methods (addEntropy and read in the base PRNG class, and the constructor for ParametersWithIV) to accept void[] in place of ubyte[].
Thomas Dixon <reikon@reikon.us>
parents:
30
diff
changeset
|
40 version (D_Version2) |
2b4bccdc8387
Added version() statements to play nice with D2's current feelings about const. Changed a few methods (addEntropy and read in the base PRNG class, and the constructor for ParametersWithIV) to accept void[] in place of ubyte[].
Thomas Dixon <reikon@reikon.us>
parents:
30
diff
changeset
|
41 { |
2b4bccdc8387
Added version() statements to play nice with D2's current feelings about const. Changed a few methods (addEntropy and read in the base PRNG class, and the constructor for ParametersWithIV) to accept void[] in place of ubyte[].
Thomas Dixon <reikon@reikon.us>
parents:
30
diff
changeset
|
42 this (string input_) |
2b4bccdc8387
Added version() statements to play nice with D2's current feelings about const. Changed a few methods (addEntropy and read in the base PRNG class, and the constructor for ParametersWithIV) to accept void[] in place of ubyte[].
Thomas Dixon <reikon@reikon.us>
parents:
30
diff
changeset
|
43 { |
33
b9f8aa42a547
More changes suggested by Glenn Haecker for D2 compatibility.
Thomas Dixon <reikon@reikon.us>
parents:
32
diff
changeset
|
44 this(cast(ubyte[])input_); |
32
2b4bccdc8387
Added version() statements to play nice with D2's current feelings about const. Changed a few methods (addEntropy and read in the base PRNG class, and the constructor for ParametersWithIV) to accept void[] in place of ubyte[].
Thomas Dixon <reikon@reikon.us>
parents:
30
diff
changeset
|
45 } |
2b4bccdc8387
Added version() statements to play nice with D2's current feelings about const. Changed a few methods (addEntropy and read in the base PRNG class, and the constructor for ParametersWithIV) to accept void[] in place of ubyte[].
Thomas Dixon <reikon@reikon.us>
parents:
30
diff
changeset
|
46 } |
2b4bccdc8387
Added version() statements to play nice with D2's current feelings about const. Changed a few methods (addEntropy and read in the base PRNG class, and the constructor for ParametersWithIV) to accept void[] in place of ubyte[].
Thomas Dixon <reikon@reikon.us>
parents:
30
diff
changeset
|
47 |
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
|
48 /** 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
|
49 abstract uint blockSize(); |
2
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
50 |
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
|
51 /** 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
|
52 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
|
53 |
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 /** Returns: The name of the algorithm we're implementing. */ |
28
ad687db713a4
Further reworked the code for hash padding. Replaced all instances of 'char[]' with 'string' and removed a few 'const' modifiers as per Glenn Haecker's patch for D2 compatibility. Updated CONTRIBUTORS file.
Thomas Dixon <reikon@reikon.us>
parents:
27
diff
changeset
|
55 abstract string name(); |
2
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
56 |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
57 /** 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
|
58 abstract Hash copy(); |
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
59 |
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
|
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 * 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
|
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 * 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
|
64 * 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
|
65 * |
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
|
66 * 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
|
67 */ |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
68 Hash update(void[] input_) |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
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 ubyte[] input = cast(ubyte[]) input_; |
30
21847420b1ac
Changed Hash's update method to a more optimized variant. Changed the code style for the entire misc package (completely forgot about it). Further changes for D2 compatibility. It appears as if full compatibility won't be possibledue to D2's handling of void[], but the number of changes to obtain compatibility can be minimized in the least.
Thomas Dixon <reikon@reikon.us>
parents:
29
diff
changeset
|
71 uint i, partLength; |
21847420b1ac
Changed Hash's update method to a more optimized variant. Changed the code style for the entire misc package (completely forgot about it). Further changes for D2 compatibility. It appears as if full compatibility won't be possibledue to D2's handling of void[], but the number of changes to obtain compatibility can be minimized in the least.
Thomas Dixon <reikon@reikon.us>
parents:
29
diff
changeset
|
72 |
21847420b1ac
Changed Hash's update method to a more optimized variant. Changed the code style for the entire misc package (completely forgot about it). Further changes for D2 compatibility. It appears as if full compatibility won't be possibledue to D2's handling of void[], but the number of changes to obtain compatibility can be minimized in the least.
Thomas Dixon <reikon@reikon.us>
parents:
29
diff
changeset
|
73 index = bytes & (blockSize - 1); |
21847420b1ac
Changed Hash's update method to a more optimized variant. Changed the code style for the entire misc package (completely forgot about it). Further changes for D2 compatibility. It appears as if full compatibility won't be possibledue to D2's handling of void[], but the number of changes to obtain compatibility can be minimized in the least.
Thomas Dixon <reikon@reikon.us>
parents:
29
diff
changeset
|
74 bytes += input.length; |
21847420b1ac
Changed Hash's update method to a more optimized variant. Changed the code style for the entire misc package (completely forgot about it). Further changes for D2 compatibility. It appears as if full compatibility won't be possibledue to D2's handling of void[], but the number of changes to obtain compatibility can be minimized in the least.
Thomas Dixon <reikon@reikon.us>
parents:
29
diff
changeset
|
75 |
21847420b1ac
Changed Hash's update method to a more optimized variant. Changed the code style for the entire misc package (completely forgot about it). Further changes for D2 compatibility. It appears as if full compatibility won't be possibledue to D2's handling of void[], but the number of changes to obtain compatibility can be minimized in the least.
Thomas Dixon <reikon@reikon.us>
parents:
29
diff
changeset
|
76 partLength = blockSize - index; |
21847420b1ac
Changed Hash's update method to a more optimized variant. Changed the code style for the entire misc package (completely forgot about it). Further changes for D2 compatibility. It appears as if full compatibility won't be possibledue to D2's handling of void[], but the number of changes to obtain compatibility can be minimized in the least.
Thomas Dixon <reikon@reikon.us>
parents:
29
diff
changeset
|
77 |
21847420b1ac
Changed Hash's update method to a more optimized variant. Changed the code style for the entire misc package (completely forgot about it). Further changes for D2 compatibility. It appears as if full compatibility won't be possibledue to D2's handling of void[], but the number of changes to obtain compatibility can be minimized in the least.
Thomas Dixon <reikon@reikon.us>
parents:
29
diff
changeset
|
78 if (input.length >= partLength) |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
79 { |
30
21847420b1ac
Changed Hash's update method to a more optimized variant. Changed the code style for the entire misc package (completely forgot about it). Further changes for D2 compatibility. It appears as if full compatibility won't be possibledue to D2's handling of void[], but the number of changes to obtain compatibility can be minimized in the least.
Thomas Dixon <reikon@reikon.us>
parents:
29
diff
changeset
|
80 buffer[index..index+partLength] = input[0..partLength]; |
21847420b1ac
Changed Hash's update method to a more optimized variant. Changed the code style for the entire misc package (completely forgot about it). Further changes for D2 compatibility. It appears as if full compatibility won't be possibledue to D2's handling of void[], but the number of changes to obtain compatibility can be minimized in the least.
Thomas Dixon <reikon@reikon.us>
parents:
29
diff
changeset
|
81 transform(buffer); |
21847420b1ac
Changed Hash's update method to a more optimized variant. Changed the code style for the entire misc package (completely forgot about it). Further changes for D2 compatibility. It appears as if full compatibility won't be possibledue to D2's handling of void[], but the number of changes to obtain compatibility can be minimized in the least.
Thomas Dixon <reikon@reikon.us>
parents:
29
diff
changeset
|
82 |
21847420b1ac
Changed Hash's update method to a more optimized variant. Changed the code style for the entire misc package (completely forgot about it). Further changes for D2 compatibility. It appears as if full compatibility won't be possibledue to D2's handling of void[], but the number of changes to obtain compatibility can be minimized in the least.
Thomas Dixon <reikon@reikon.us>
parents:
29
diff
changeset
|
83 for (i = partLength; i + (blockSize - 1) < input.length; i+=blockSize) |
21847420b1ac
Changed Hash's update method to a more optimized variant. Changed the code style for the entire misc package (completely forgot about it). Further changes for D2 compatibility. It appears as if full compatibility won't be possibledue to D2's handling of void[], but the number of changes to obtain compatibility can be minimized in the least.
Thomas Dixon <reikon@reikon.us>
parents:
29
diff
changeset
|
84 transform(input[i..i+blockSize]); |
21847420b1ac
Changed Hash's update method to a more optimized variant. Changed the code style for the entire misc package (completely forgot about it). Further changes for D2 compatibility. It appears as if full compatibility won't be possibledue to D2's handling of void[], but the number of changes to obtain compatibility can be minimized in the least.
Thomas Dixon <reikon@reikon.us>
parents:
29
diff
changeset
|
85 |
21847420b1ac
Changed Hash's update method to a more optimized variant. Changed the code style for the entire misc package (completely forgot about it). Further changes for D2 compatibility. It appears as if full compatibility won't be possibledue to D2's handling of void[], but the number of changes to obtain compatibility can be minimized in the least.
Thomas Dixon <reikon@reikon.us>
parents:
29
diff
changeset
|
86 index = 0; |
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
|
87 } |
30
21847420b1ac
Changed Hash's update method to a more optimized variant. Changed the code style for the entire misc package (completely forgot about it). Further changes for D2 compatibility. It appears as if full compatibility won't be possibledue to D2's handling of void[], but the number of changes to obtain compatibility can be minimized in the least.
Thomas Dixon <reikon@reikon.us>
parents:
29
diff
changeset
|
88 else |
21847420b1ac
Changed Hash's update method to a more optimized variant. Changed the code style for the entire misc package (completely forgot about it). Further changes for D2 compatibility. It appears as if full compatibility won't be possibledue to D2's handling of void[], but the number of changes to obtain compatibility can be minimized in the least.
Thomas Dixon <reikon@reikon.us>
parents:
29
diff
changeset
|
89 i = 0; |
21847420b1ac
Changed Hash's update method to a more optimized variant. Changed the code style for the entire misc package (completely forgot about it). Further changes for D2 compatibility. It appears as if full compatibility won't be possibledue to D2's handling of void[], but the number of changes to obtain compatibility can be minimized in the least.
Thomas Dixon <reikon@reikon.us>
parents:
29
diff
changeset
|
90 |
21847420b1ac
Changed Hash's update method to a more optimized variant. Changed the code style for the entire misc package (completely forgot about it). Further changes for D2 compatibility. It appears as if full compatibility won't be possibledue to D2's handling of void[], but the number of changes to obtain compatibility can be minimized in the least.
Thomas Dixon <reikon@reikon.us>
parents:
29
diff
changeset
|
91 if (input.length - i) |
21847420b1ac
Changed Hash's update method to a more optimized variant. Changed the code style for the entire misc package (completely forgot about it). Further changes for D2 compatibility. It appears as if full compatibility won't be possibledue to D2's handling of void[], but the number of changes to obtain compatibility can be minimized in the least.
Thomas Dixon <reikon@reikon.us>
parents:
29
diff
changeset
|
92 buffer[index..index+(input.length-i)] = input[i..input.length]; |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
93 |
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
|
94 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
|
95 } |
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 |
32
2b4bccdc8387
Added version() statements to play nice with D2's current feelings about const. Changed a few methods (addEntropy and read in the base PRNG class, and the constructor for ParametersWithIV) to accept void[] in place of ubyte[].
Thomas Dixon <reikon@reikon.us>
parents:
30
diff
changeset
|
97 /** Play nice with D2's idea of const. */ |
2b4bccdc8387
Added version() statements to play nice with D2's current feelings about const. Changed a few methods (addEntropy and read in the base PRNG class, and the constructor for ParametersWithIV) to accept void[] in place of ubyte[].
Thomas Dixon <reikon@reikon.us>
parents:
30
diff
changeset
|
98 version (D_Version2) |
2b4bccdc8387
Added version() statements to play nice with D2's current feelings about const. Changed a few methods (addEntropy and read in the base PRNG class, and the constructor for ParametersWithIV) to accept void[] in place of ubyte[].
Thomas Dixon <reikon@reikon.us>
parents:
30
diff
changeset
|
99 { |
2b4bccdc8387
Added version() statements to play nice with D2's current feelings about const. Changed a few methods (addEntropy and read in the base PRNG class, and the constructor for ParametersWithIV) to accept void[] in place of ubyte[].
Thomas Dixon <reikon@reikon.us>
parents:
30
diff
changeset
|
100 Hash update(string input_) |
2b4bccdc8387
Added version() statements to play nice with D2's current feelings about const. Changed a few methods (addEntropy and read in the base PRNG class, and the constructor for ParametersWithIV) to accept void[] in place of ubyte[].
Thomas Dixon <reikon@reikon.us>
parents:
30
diff
changeset
|
101 { |
2b4bccdc8387
Added version() statements to play nice with D2's current feelings about const. Changed a few methods (addEntropy and read in the base PRNG class, and the constructor for ParametersWithIV) to accept void[] in place of ubyte[].
Thomas Dixon <reikon@reikon.us>
parents:
30
diff
changeset
|
102 return update(cast(ubyte[])input_); |
2b4bccdc8387
Added version() statements to play nice with D2's current feelings about const. Changed a few methods (addEntropy and read in the base PRNG class, and the constructor for ParametersWithIV) to accept void[] in place of ubyte[].
Thomas Dixon <reikon@reikon.us>
parents:
30
diff
changeset
|
103 } |
2b4bccdc8387
Added version() statements to play nice with D2's current feelings about const. Changed a few methods (addEntropy and read in the base PRNG class, and the constructor for ParametersWithIV) to accept void[] in place of ubyte[].
Thomas Dixon <reikon@reikon.us>
parents:
30
diff
changeset
|
104 } |
2b4bccdc8387
Added version() statements to play nice with D2's current feelings about const. Changed a few methods (addEntropy and read in the base PRNG class, and the constructor for ParametersWithIV) to accept void[] in place of ubyte[].
Thomas Dixon <reikon@reikon.us>
parents:
30
diff
changeset
|
105 |
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
|
106 /** 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
|
107 protected abstract void transform(ubyte[] input); |
0 | 108 |
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
|
109 /** |
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
|
110 * 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
|
111 * |
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
|
112 * 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
|
113 * 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
|
114 * 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
|
115 */ |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
116 protected void padMessage(uint mode) |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
117 { |
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
|
118 ulong bits = bytes << 3; |
30
21847420b1ac
Changed Hash's update method to a more optimized variant. Changed the code style for the entire misc package (completely forgot about it). Further changes for D2 compatibility. It appears as if full compatibility won't be possibledue to D2's handling of void[], but the number of changes to obtain compatibility can be minimized in the least.
Thomas Dixon <reikon@reikon.us>
parents:
29
diff
changeset
|
119 index = bytes & (blockSize - 1); |
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
|
120 |
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
|
121 // 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
|
122 buffer[index++] = ((mode == MODE_TIGER) ? 0x01 : 0x80); |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
123 if (index == blockSize) |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
124 { |
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
|
125 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
|
126 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
|
127 } |
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
|
128 |
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
|
129 // Pad with null bytes |
28
ad687db713a4
Further reworked the code for hash padding. Replaced all instances of 'char[]' with 'string' and removed a few 'const' modifiers as per Glenn Haecker's patch for D2 compatibility. Updated CONTRIBUTORS file.
Thomas Dixon <reikon@reikon.us>
parents:
27
diff
changeset
|
130 while ((index & (blockSize - 1)) != (blockSize - (blockSize >> 3))) |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
131 { |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
132 buffer[index++] = 0; |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
133 |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
134 if (index == blockSize) |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
135 { |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
136 transform(buffer); |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
137 index = 0; |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
138 } |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
139 } |
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
|
140 |
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
|
141 // Length padding |
28
ad687db713a4
Further reworked the code for hash padding. Replaced all instances of 'char[]' with 'string' and removed a few 'const' modifiers as per Glenn Haecker's patch for D2 compatibility. Updated CONTRIBUTORS file.
Thomas Dixon <reikon@reikon.us>
parents:
27
diff
changeset
|
142 for (int i = 0; i < blockSize; i+=8, bits>>=8) // little endian |
ad687db713a4
Further reworked the code for hash padding. Replaced all instances of 'char[]' with 'string' and removed a few 'const' modifiers as per Glenn Haecker's patch for D2 compatibility. Updated CONTRIBUTORS file.
Thomas Dixon <reikon@reikon.us>
parents:
27
diff
changeset
|
143 buffer[index++] = bits; |
ad687db713a4
Further reworked the code for hash padding. Replaced all instances of 'char[]' with 'string' and removed a few 'const' modifiers as per Glenn Haecker's patch for D2 compatibility. Updated CONTRIBUTORS file.
Thomas Dixon <reikon@reikon.us>
parents:
27
diff
changeset
|
144 |
0 | 145 if (mode == MODE_SHA) |
28
ad687db713a4
Further reworked the code for hash padding. Replaced all instances of 'char[]' with 'string' and removed a few 'const' modifiers as per Glenn Haecker's patch for D2 compatibility. Updated CONTRIBUTORS file.
Thomas Dixon <reikon@reikon.us>
parents:
27
diff
changeset
|
146 buffer[(buffer.length-(blockSize >> 3))..buffer.length].reverse; // big endian |
ad687db713a4
Further reworked the code for hash padding. Replaced all instances of 'char[]' with 'string' and removed a few 'const' modifiers as per Glenn Haecker's patch for D2 compatibility. Updated CONTRIBUTORS file.
Thomas Dixon <reikon@reikon.us>
parents:
27
diff
changeset
|
147 |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
148 transform(buffer); |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
149 index = 0; |
0 | 150 } |
151 | |
152 /** | |
153 * Process all data, pad and finalize. This method will | |
14
5ce3012f1def
Removed some redundancy in code. Added NotSupportedError, a base PRNG class and a class which creates a PRNG from a hash function. Changed the MAC class' finalization methods to digest and hexDigest instead of finish and hexFinish respectively. Also added a base Checksum class, crc32 and adler32 in dcrypt.misc as per request.
Thomas Dixon <reikon@reikon.us>
parents:
12
diff
changeset
|
154 * reset the digest to its original state for subsequent use. |
0 | 155 * |
156 * Returns: Binary representation of the hash in bytes. | |
157 */ | |
158 abstract ubyte[] digest(); | |
159 | |
160 /** | |
161 * Same as digest() but returns hash value in hex. | |
162 * | |
163 * Returns: Representation of the final hash value in hex. | |
164 */ | |
33
b9f8aa42a547
More changes suggested by Glenn Haecker for D2 compatibility.
Thomas Dixon <reikon@reikon.us>
parents:
32
diff
changeset
|
165 string hexDigest() |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
166 { |
23
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
14
diff
changeset
|
167 return ByteConverter.hexEncode(digest()); |
0 | 168 } |
169 | |
170 /** Reset hash to initial state. */ | |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
171 void reset() |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
23
diff
changeset
|
172 { |
0 | 173 bytes = index = 0; |
174 } | |
175 } |