/// <summary> /// Hash a string data with HMAC SHA256. /// </summary> /// <param name="data"> a string to be hashed </param> /// <returns> a string of hash code </returns> public static string HashWithSHA256(string data) { using (SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider()) { return(StringUtilityService.BytesToHexString(sha256.ComputeHash(Encoding.ASCII.GetBytes(data)))); } }
/// <summary> /// Hashes the string with SHA1. /// </summary> /// <param name="str">The input to be hashed </param> /// <returns>Hex string of the hashed input</returns> public static string HashWithSHA1(string str) { using (SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider()) { // Convert the str to ASCII byte array -> // Compute the hashcode in byte array with the ASCII byte array -> // Convert the hashcode byte array to a hex string return(StringUtilityService.BytesToHexString(sha1.ComputeHash(Encoding.ASCII.GetBytes(str)))); } }
/// <summary> /// Hash a password with SHA3-256 KDF. /// </summary> /// <param name="password"> password to be hashed</param> /// <param name="salt"> salt used for hashing </param> /// <param name="iterations"> number of iterations </param> /// <param name="hashLength"> the length of the output hashcode </param> /// <returns> a string of derived key </returns> public static string HashWithKDF(string password, byte[] salt, int iterations = Constants.DefaultHashIterations, int hashLength = Constants.DefaultHashByteLength) { byte[] passwordBytes = StringUtilityService.HexStringToBytes(password); Pkcs5S2ParametersGenerator pbkdf = new Pkcs5S2ParametersGenerator(new Sha3Digest()); pbkdf.Init(passwordBytes, salt, iterations); KeyParameter derivedKey = pbkdf.GenerateDerivedMacParameters(hashLength * Constants.ByteLength) as KeyParameter; return(StringUtilityService.BytesToHexString(derivedKey.GetKey())); }