/// <summary> /// Calculate a unique hash for a non-null series of bytes (Uses SHA256 by default) /// </summary> /// <param name="bytesToHash">Non-null list of bytes upon which to calculate hash</param> /// <param name="hashSalt">(optional) Additional text that you want to use to add addtional 'secrecy' to your hash. If provided uses HMAC256 instead of SHA256</param> /// <returns>Base64 encoded string of hash. The value is suitable for storing in databases but might need to be HtmlEncoded if used in query strings or displayed on web pages</returns> public static byte[] Hash(byte[] bytesToHash, byte[] hashSalt = null) { if (hashSalt == null) { var simpleKey = HashKeySimple.Create(_defaultSimpleHashMode); return(simpleKey.Hash(bytesToHash)); } var keyedHasher = HashKeySalted.Create(_defaultSaltedHashMode, hashSalt); return(keyedHasher.Hash(bytesToHash)); }
/// <summary> /// Create a key suitable for a hashing using a specific algorithm /// </summary> /// <param name="hashMode">One of the HashModesSimple constants</param> /// <returns>New hash key</returns> public static HashKeySimple Create(string hashMode) { var key = new HashKeySimple(); key.Properties.Add(new KeyProperty() { PropertyType = Crypto.PropertyTypes.HashMode, Value = hashMode, ValueType = Crypto.PropertyValueTypes.String }); return(key); }
/// <summary> /// Create a key suitable for a hashing using a specific algorithm /// </summary> /// <param name="hashMode">One of the HashModesSimple constants</param> /// <returns>New hash key</returns> public static HashKeySimple Create(string hashMode) { var key = new HashKeySimple(); key.Properties.Add(new KeyProperty() { PropertyType = Crypto.PropertyNames.HashSimpleMode, Value = hashMode, ValueType = Crypto.PropertyValueTypes.String }); return key; }