/// <summary> /// Computes the hash value for the specified string. /// </summary> /// <param name="hashType">The type of algorithm to use when computing the hash.</param> /// <param name="value">The input to compute the hash code for.</param> /// <param name="encoding"> /// The encoding to use when retrieving the binary representation of <paramref name="value"/>. /// If null or not supplied, then <see cref="Encoding.UTF8"/> is used instead. /// </param> /// <returns>The computed hash code.</returns> public static string ComputeHash(this HashType hashType, string value, Encoding encoding = null) { if (value == null) { throw new ArgumentNullException("value"); } encoding = encoding ?? Encoding.UTF8; return(hashType.ComputeHash(encoding.GetBytes(value))); }
/// <summary> /// 获取Hash后的字节数组 /// </summary> /// <param name="type">哈希类型</param> /// <param name="key">key</param> /// <param name="bytes">原字节数组</param> /// <returns></returns> public static byte[] GetHashedBytes(HashType type, byte[] bytes, byte[]?key) { Guard.NotNull(bytes, nameof(bytes)); if (bytes.Length == 0) { return(bytes); } HashAlgorithm algorithm = null !; try { if (key == null) { algorithm = type switch { HashType.SHA1 => SHA1.Create(), HashType.SHA256 => SHA256.Create(), HashType.SHA384 => SHA384.Create(), HashType.SHA512 => SHA512.Create(), _ => MD5.Create() }; } else { algorithm = type switch { HashType.SHA1 => new HMACSHA1(key), HashType.SHA256 => new HMACSHA256(key), HashType.SHA384 => new HMACSHA384(key), HashType.SHA512 => new HMACSHA512(key), _ => new HMACMD5(key) }; } return(algorithm.ComputeHash(bytes)); } finally { algorithm.Dispose(); } }
/// <summary> /// Computes the hash value for the specified byte array. /// </summary> /// <param name="hashType">The type of algorithm to use when computing the hash.</param> /// <param name="data">The input to compute the hash code for.</param> /// <returns>The computed hash code.</returns> public static string ToHash(this byte[] data, HashType hashType) { return hashType.ComputeHash(data); }
/// <summary> /// Converts a string to a Hash of the specified type. /// </summary> /// <param name="value">The string to be hashed.</param> /// <param name="hashType">Type of the hash.</param> /// <returns>The hash value</returns> public static string ToHash(this string value, HashType hashType) { return hashType.ComputeHash(value); }
/// <summary> /// Converts a string to a Hash of the specified type. /// </summary> /// <param name="value">The string to be hashed.</param> /// <param name="hashType">Type of the hash.</param> /// <returns>The hash value</returns> public static string ToHash(this string value, HashType hashType) { return(hashType.ComputeHash(value)); }
/// <summary> /// Computes the hash value for the specified <see cref="Stream"/> object. /// </summary> /// <param name="hashType">The type of algorithm to use when computing the hash.</param> /// <param name="inputStream">The input to compute the hash code for.</param> /// <returns>The computed hash code.</returns> public static string ComputeHash(this HashType hashType, Stream inputStream) { return(hashType.ComputeHash(inputStream, false)); }
/// <summary> /// Computes the hash value for the specified byte array. /// </summary> /// <param name="hashType">The type of algorithm to use when computing the hash.</param> /// <param name="data">The input to compute the hash code for.</param> /// <returns>The computed hash code.</returns> public static string ToHash(this byte[] data, HashType hashType) { return(hashType.ComputeHash(data)); }