Esempio n. 1
0
        /// <summary>
        /// SHA-256
        /// </summary>
        /// <param name="str"></param>
        public static string SHA_256(string str)
        {
            System.Security.Cryptography.SHA256CryptoServiceProvider sha256Csp = new System.Security.Cryptography.SHA256CryptoServiceProvider();
            byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(str);
            byte[] bytHash  = sha256Csp.ComputeHash(bytValue);
            sha256Csp.Clear();
            string hashStr = "";

            for (int counter = 0; counter < bytHash.Count(); counter++)
            {
                long i       = bytHash[counter] / 16;
                var  tempStr = "";
                if (i > 9)
                {
                    tempStr = ((char)(i - 10 + 0x41)).ToString();
                }
                else
                {
                    tempStr = ((char)(i + 0x30)).ToString();
                }
                i = bytHash[counter] % 16;
                if (i > 9)
                {
                    tempStr += ((char)(i - 10 + 0x41)).ToString();
                }
                else
                {
                    tempStr += ((char)(i + 0x30)).ToString();
                }
                hashStr += tempStr;
            }
            return(hashStr);
        }
Esempio n. 2
0
 public string SHA256(string str)
 {
     var sha256 = new System.Security.Cryptography.SHA256CryptoServiceProvider();
     var bs = sha256.ComputeHash(System.Text.Encoding.UTF8.GetBytes(str));
     sha256.Clear();
     return bs.Aggregate("", (current, b) => current + (b.ToString("x2")));
 }
Esempio n. 3
0
        /// <summary>
        /// 计算SHA-256码
        /// </summary>
        /// <param name="word">字符串</param>
        /// <param name="toUpper">返回哈希值格式 true:英文大写,false:英文小写</param>
        /// <returns></returns>
        public string Hash_SHA_256(string word, bool toUpper = true)
        {
            try
            {
                System.Security.Cryptography.SHA256CryptoServiceProvider SHA256CSP
                    = new System.Security.Cryptography.SHA256CryptoServiceProvider();

                byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word);
                byte[] bytHash  = SHA256CSP.ComputeHash(bytValue);
                SHA256CSP.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);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// 计算SHA-256码
        /// </summary>
        /// <param name="word">字符串</param>
        /// <param name="toUpper">返回哈希值格式 true:英文大写,false:英文小写</param>
        /// <returns></returns>
        public static string Hash_SHA_256(string word, bool toUpper = true)
        {
            try
            {
                System.Security.Cryptography.SHA256CryptoServiceProvider SHA256CSP
                    = new System.Security.Cryptography.SHA256CryptoServiceProvider();

                byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word);
                byte[] bytHash = SHA256CSP.ComputeHash(bytValue);
                SHA256CSP.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);
            }
        }
Esempio n. 5
0
        public void Calc_SHA256()
        {
            System.IO.FileStream fs = new System.IO.FileStream(
                filepath,
                System.IO.FileMode.Open,
                System.IO.FileAccess.Read,
                System.IO.FileShare.Read);

            System.Security.Cryptography.SHA256CryptoServiceProvider sha256
                = new System.Security.Cryptography.SHA256CryptoServiceProvider();

            byte[] bs = sha256.ComputeHash(fs);

            sha256.Clear();
            fs.Close();

            SHA256_hash    = BitConverter.ToString(bs).ToLower().Replace("-", "");
            virustotal_url = "https://www.virustotal.com/ja/file/" + SHA256_hash + "/analysis/";
        }