Mercurial > projects > dcrypt
diff dcrypt/crypto/macs/HMAC.d @ 15:0de48552be35
Added LimitReachedError and PBKDF2. Fixed some errors with the previous commit in PRNGFromHash, etc. Re-implemented HMAC. Changed the name() format of HMAC and PBKDF2.
author | Thomas Dixon <reikon@reikon.us> |
---|---|
date | Wed, 19 Nov 2008 19:30:52 -0500 |
parents | 5ce3012f1def |
children | 4589f8c5eb3c |
line wrap: on
line diff
--- a/dcrypt/crypto/macs/HMAC.d Tue Nov 18 18:03:40 2008 -0500 +++ b/dcrypt/crypto/macs/HMAC.d Wed Nov 19 19:30:52 2008 -0500 @@ -26,21 +26,17 @@ class HMAC : MAC { private { ubyte[] ipad, opad, key; - Hash inner, outer; + Hash hash; bool initialized; } this (Hash hash, void[] key=null) { - hash.reset(); - - inner = hash; - outer = hash.copy(); + this.hash = hash.copy(); + this.hash.reset(); ipad = new ubyte[blockSize]; opad = new ubyte[blockSize]; - reset(); - if (key) init(new SymmetricKey(key)); // I'm lazy. } @@ -51,19 +47,23 @@ throw new InvalidParameterError( name()~": Invalid parameter passed to init"); + hash.reset(); + if (keyParams.key.length > blockSize) { - inner.update(keyParams.key); - key = inner.digest(); + hash.update(keyParams.key); + key = hash.digest(); } else key = keyParams.key; + ipad[] = 0x36; + opad[] = 0x5c; + foreach (uint i, ubyte j; key) { ipad[i] ^= j; opad[i] ^= j; } - inner.update(ipad); - outer.update(opad); + reset(); initialized = true; } @@ -72,45 +72,43 @@ if (!initialized) throw new NotInitializedError( name()~": MAC not initialized."); - inner.update(input_); + hash.update(input_); } char[] name() { - return inner.name~"/HMAC"; + return "HMAC-"~hash.name; } - void reset() { - ipad[] = 0x36; - opad[] = 0x5c; - - inner.reset(); - outer.reset(); + void reset() { + hash.reset(); + hash.update(ipad); } uint blockSize() { - return inner.blockSize; + return hash.blockSize; } uint macSize() { - return inner.digestSize; + return hash.digestSize; } ubyte[] digest() { - outer.update(inner.digest()); - ubyte[] r = outer.digest(); + ubyte[] t = hash.digest(); + hash.update(opad); + hash.update(t); + ubyte[] r = hash.digest(); reset(); return r; } char[] hexDigest() { - return Util.ubytesToHex(finish()); + return Util.ubytesToHex(digest()); } HMAC copy() { // Ghetto... oh so ghetto :\ - HMAC h = new HMAC(inner.copy()); - h.inner = inner.copy(); - h.outer = outer.copy(); + HMAC h = new HMAC(hash.copy()); + h.hash = hash.copy(); h.initialized = true; return h; }