예제 #1
0
 /// <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));
 }
예제 #2
0
        /// <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 == null)
            {
                throw new ArgumentNullException(nameof(name), "name should not be null.");
            }
            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));
            }
            switch (name.Name.ToUpperInvariant().Replace("-", string.Empty))
            {
            case "SHA3512":
            case "KECCAK512":
            case "SHA3":
                return(SHA3Managed.Create512());

            case "SHA3384":
            case "KECCAK384":
                return(SHA3Managed.Create384());

            case "SHA3256":
            case "KECCAK256":
                return(SHA3Managed.Create256());

            case "SHA3224":
            case "KECCAK224":
                return(SHA3Managed.Create224());

            case "SHA256":
                return(SHA256.Create());

            case "SHA384":
                return(SHA384.Create());

            case "SHA512":
            case "SHA2":
                return(SHA512.Create());

            case "MD5":
                return(MD5.Create());

            case "SHA1":
                return(SHA1.Create());
            }

            return(HashAlgorithm.Create(name.Name));
        }