/// <summary> /// Creates a hash algorithm instance. /// </summary> /// <param name="name">The hash algorithm name,</param> /// <returns>A hash algorithm instance.</returns> /// <exception cref="ArgumentNullException">name was null.</exception> /// <exception cref="ArgumentException">name.Name was null or empty.</exception> /// <exception cref="NotSupportedException">The hash algorithm name is not supported.</exception> public static HashAlgorithm Create(HashAlgorithmName name) { if (name == HashAlgorithmName.SHA512) { return(SHA512.Create()); } if (name == HashAlgorithmName.MD5) { return(MD5.Create()); } if (name == HashAlgorithmName.SHA256) { return(SHA256.Create()); } if (name == HashAlgorithmName.SHA1) { return(SHA1.Create()); } if (name == HashAlgorithmName.SHA384) { return(SHA384.Create()); } if (string.IsNullOrWhiteSpace(name.Name)) { throw new ArgumentException("name.Name should not be null or empty.", nameof(name)); } return((name.Name.ToUpperInvariant().Replace("-", string.Empty)) switch { "SHA3512" or "KECCAK512" or "SHA3" => SHA3Managed.Create512(), "SHA3384" or "KECCAK384" => SHA3Managed.Create384(), "SHA3256" or "KECCAK256" => SHA3Managed.Create256(), "SHA3224" or "KECCAK224" => SHA3Managed.Create224(), "SHA256" => SHA256.Create(), "SHA384" => SHA384.Create(), "SHA512" or "SHA2" => SHA512.Create(), "MD5" => MD5.Create(), "SHA1" => SHA1.Create(), _ => HashAlgorithm.Create(name.Name), });
/// <summary> /// Creates a hash signature provider using SHA-3-256 hash algorithm. /// </summary> /// <param name="shortName">true if use short name; otherwise, false.</param> /// <returns>A keyed hash signature provider.</returns> public static HashSignatureProvider CreateSHA3256(bool shortName = false) { return(new HashSignatureProvider(SHA3Managed.Create256(), shortName ? "S3256" : "SHA3256", true)); }