// -------------------------------------------------------------------------------- // Private definition of the hash operation used by this class. It is: // // SHA1( message-text + random-salt + static-salt ) // // NOTE: We convert the inputted byte arrays into strings for code simplicity. // (Probably slower than assembling a byte buffer with byte array copies.) // -------------------------------------------------------------------------------- private static byte[] _hash3(byte[] text, byte[] saltR, byte[] saltS, Krypto.HashAlgoID algo) { switch (algo) { case Krypto.HashAlgoID.SHA1: return Krypto.hash_sha1(ArrayOp.Append<byte>(text, saltR, saltS)); case Krypto.HashAlgoID.PBKDF2: return Krypto.PBKDF2( text, ArrayOp.Append<byte>(saltR, saltS), 2112, 32); default: throw new System.ArgumentException( "_hash3: invalid HashAlgoID", String.Format( "{0}",algo )); } }
// -------------------------------------------------------------------------------- // Creates the hash and returns // NOTE: Why didn't I made this a constructor instead of a static function? // -------------------------------------------------------------------------------- public static HashStore Create(byte[] text, byte[] saltStatic, Krypto.HashAlgoID algo) { // Create a new random salt for every hash store instance. byte[] saltRandom = Krypto.RandomBytes(16); // Hash a string made from the random salt, static salt, and text. byte[] hash = _hash3(text, saltRandom, saltStatic, algo); // Return a key store containing both the hash and the random salt. return new HashStore(hash, saltRandom, algo); }
public static HashStore Create(string text, byte[] saltStatic, Krypto.HashAlgoID algo) { return Create(Encoding.UTF8.GetBytes(text), saltStatic, algo); }
// private constructor for packing pieces parts together. private HashStore(byte[] hash, byte[] salt, Krypto.HashAlgoID algo) { this.hash = hash; this.salt = salt; this.algo = algo; }