Mercurial > projects > dcrypt
diff dcrypt/crypto/prngs/PRNGFromHash.d @ 27:8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
author | Thomas Dixon <reikon@reikon.us> |
---|---|
date | Sat, 09 May 2009 23:29:20 -0400 |
parents | 703901987976 |
children | ad687db713a4 |
line wrap: on
line diff
--- a/dcrypt/crypto/prngs/PRNGFromHash.d Sun Mar 01 13:06:48 2009 -0500 +++ b/dcrypt/crypto/prngs/PRNGFromHash.d Sat May 09 23:29:20 2009 -0400 @@ -12,8 +12,10 @@ import dcrypt.crypto.Hash; /** Creates a PRNG from a hash function. */ -class PRNGFromHash : PRNG { - private { +class PRNGFromHash : PRNG +{ + private + { const uint COUNTER_SIZE = 32; Hash hash; @@ -23,17 +25,20 @@ uint index; } - char[] name() { + char[] name() + { if (hash is null) throw new NotInitializedError(name()~": PRNG not initialized"); return hash.name~"PRNG"; } - this(Hash hash) { + this(Hash hash) + { if (hash is null) throw new InvalidParameterError( name()~": Invalid parameter passed to constructor."); + this.hash = hash; this.hash.reset(); @@ -44,8 +49,10 @@ index = this.hash.digestSize; // to force updating of the state } - void addEntropy(ubyte[] input) { - if (!_initialized) { + void addEntropy(ubyte[] input) + { + if (!_initialized) + { hash.update(input); seed = hash.digest(); _initialized = true; @@ -53,12 +60,15 @@ throw new NotSupportedError(name()~": state is immutable once initialized"); } - uint read(ubyte[] output) { + uint read(ubyte[] output) + { if (!_initialized) throw new NotInitializedError(name()~": PRNG not initialized"); - for (uint i = 0; i < output.length; i++) { - if (index == state.length) { + for (uint i = 0; i < output.length; i++) + { + if (index == state.length) + { hash.update(seed); hash.update(counter); state = hash.digest(); @@ -71,6 +81,7 @@ } output[i] = state[index++]; } + return output.length; } -} \ No newline at end of file +}