Пример #1
0
        /// <summary>
        /// 计算SHA-512码
        /// </summary>
        /// <param name="word">字符串</param>
        /// <param name="toUpper">返回哈希值格式 true:英文大写,false:英文小写</param>
        /// <returns></returns>
        public static string Hash_SHA_512(string word, bool toUpper = true)
        {
            try
            {
                System.Security.Cryptography.SHA512CryptoServiceProvider SHA512CSP
                    = new System.Security.Cryptography.SHA512CryptoServiceProvider();

                byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word);
                byte[] bytHash  = SHA512CSP.ComputeHash(bytValue);
                SHA512CSP.Clear();

                //根据计算得到的Hash码翻译为SHA-1码
                string sHash = "", sTemp = "";
                for (int counter = 0; counter < bytHash.Count(); counter++)
                {
                    long i = bytHash[counter] / 16;
                    if (i > 9)
                    {
                        sTemp = ((char)(i - 10 + 0x41)).ToString();
                    }
                    else
                    {
                        sTemp = ((char)(i + 0x30)).ToString();
                    }
                    i = bytHash[counter] % 16;
                    if (i > 9)
                    {
                        sTemp += ((char)(i - 10 + 0x41)).ToString();
                    }
                    else
                    {
                        sTemp += ((char)(i + 0x30)).ToString();
                    }
                    sHash += sTemp;
                }

                //根据大小写规则决定返回的字符串
                return(toUpper ? sHash : sHash.ToLower());
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Пример #2
0
        /// <summary>
        /// 获取文本SHA512
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string GetTextSHA_512(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(string.Empty);
            }
            System.Security.Cryptography.SHA512CryptoServiceProvider SHA512CSP
                = new System.Security.Cryptography.SHA512CryptoServiceProvider();

            byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(text);
            byte[] bytHash  = SHA512CSP.ComputeHash(bytValue);
            SHA512CSP.Clear();

            //根据计算得到的Hash码翻译为SHA-1码
            string sHash = ConvertHashBytes(bytHash);

            //根据大小写规则决定返回的字符串
            return(sHash.ToLower());
        }