Beispiel #1
0
        /// <summary>
        /// Creates a SHA512 hash of the specified input.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="algo">algorithmName: MD5,SHA1,SHA256,SHA384,SHA512 default: SHA256</param>
        /// <param name="key">key</param>
        /// <param name="fromFile">是否是文件</param>
        /// <returns>A hash</returns>
        public static string HmacBase64(this string input, AlgorithmNameEnum algo = AlgorithmNameEnum.HMACSHA256, string key = null, bool fromFile = false)
        {
            if (input.IsMissing())
            {
                return(string.Empty);
            }

            if (fromFile && File.Exists(input))
            {
                using (FileStream fs = new FileStream(input, FileMode.Open))
                {
                    return(fs.HmacBase64(algo, key));
                }
            }

            using (var sha = HMAC.Create(algo.ToString()))
            {
                if (!string.IsNullOrEmpty(key))
                {
                    sha.Key = Encoding.UTF8.GetBytes(key);
                }
                var bytes = Encoding.UTF8.GetBytes(input);
                return(sha.ComputeHash(bytes).ToBase64());
            }
        }
Beispiel #2
0
        /// <summary>
        /// Creates a SHA512 hash of the specified input.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="algo">algorithmName: MD5,SHA1,SHA256,SHA384,SHA512 default: SHA256</param>
        /// <returns>A hash.</returns>
        public static string HmacBase64(this byte[] input, AlgorithmNameEnum algo = AlgorithmNameEnum.HMACSHA256)
        {
            if (input == null)
            {
                return(null);
            }

            using (var sha = HMAC.Create(algo.ToString()))
            {
                return(sha.ComputeHash(input).ToBase64());
            }
        }
Beispiel #3
0
        /// <summary>
        /// Creates a SHA512 hash of the specified input.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="algo">algorithmName: MD5,SHA1,SHA256,SHA384,SHA512 default: SHA256</param>
        /// <returns>A hash</returns>
        public static string HmacBase64(this Stream input, AlgorithmNameEnum algo = AlgorithmNameEnum.HMACSHA256)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            using (var sha = HMAC.Create(algo.ToString()))
            {
                return(sha.ComputeHash(input).ToBase64());
            }
        }
Beispiel #4
0
        /// <summary>
        /// Creates a SHA512 hash of the specified input.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="algo">algorithmName: MD5,SHA1,SHA256,SHA384,SHA512 default: SHA256</param>
        /// <param name="key">key</param>
        /// <returns>A hash.</returns>
        public static string HmacBase64(this byte[] input, AlgorithmNameEnum algo = AlgorithmNameEnum.HMACSHA256, string key = null)
        {
            if (input == null)
            {
                return(null);
            }

            using (var sha = HMAC.Create(algo.ToString()))
            {
                if (!string.IsNullOrEmpty(key))
                {
                    sha.Key = Encoding.UTF8.GetBytes(key);
                }
                return(sha.ComputeHash(input).ToBase64());
            }
        }
Beispiel #5
0
        /// <summary>
        /// Creates a SHA512 hash of the specified input.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="algo">algorithmName: MD5,SHA1,SHA256,SHA384,SHA512 default: SHA256</param>
        /// <param name="key">key</param>
        /// <returns>A hash</returns>
        public static string HmacBase64(this Stream input, AlgorithmNameEnum algo = AlgorithmNameEnum.HMACSHA256, string key = null)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            using (var sha = HMAC.Create(algo.ToString()))
            {
                if (!string.IsNullOrEmpty(key))
                {
                    sha.Key = Encoding.UTF8.GetBytes(key);
                }
                return(sha.ComputeHash(input).ToBase64());
            }
        }
Beispiel #6
0
        /// <summary>
        /// Creates a SHA512 hash of the specified input.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="algo">algorithmName: MD5,SHA1,SHA256,SHA384,SHA512 default: SHA256</param>
        /// <param name="fromFile">是否是文件</param>
        /// <returns>A hash</returns>
        public static string Hmac(this string input, AlgorithmNameEnum algo = AlgorithmNameEnum.HMACSHA256, bool fromFile = false)
        {
            if (input.IsMissing())
            {
                return(string.Empty);
            }

            if (fromFile && File.Exists(input))
            {
                using (FileStream fs = new FileStream(input, FileMode.Open))
                {
                    return(fs.Hmac(algo));
                }
            }

            using (var sha = HMAC.Create(algo.ToString()))
            {
                var bytes = Encoding.UTF8.GetBytes(input);
                return(sha.ComputeHash(bytes).Tox2String());
            }
        }