Ejemplo n.º 1
0
 /// <summary>
 /// 计算SHA-384码
 /// </summary>
 /// <param name="word">字符串</param>
 /// <param name="toUpper">返回哈希值格式 true:英文大写,false:英文小写</param>
 /// <returns></returns>
 public static string Hash_SHA_384(string word, bool toUpper = true)
 {
     try
     {
         System.Security.Cryptography.SHA384CryptoServiceProvider SHA384CSP
             = new System.Security.Cryptography.SHA384CryptoServiceProvider();
         byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word);
         byte[] bytHash  = SHA384CSP.ComputeHash(bytValue);
         SHA384CSP.Clear();
         //根据计算得到的Hash码翻译为SHA-1码
         string sHash = "", sTemp = "";
         for (int counter = 0; counter < bytHash.Length; 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);
     }
 }
Ejemplo n.º 2
0
        private void button_Hash_Click(object sender, EventArgs e)
        {
            string strResult   = "";
            string strHashData = "";

            byte[] arrbytHashValue;
            System.IO.FileStream oFileStream = null;
            if (comboBox1.Text == "")
            {
                const string message = "Please Choose a Hash Type";
                const string caption = "Hash Type";
                var          result  = MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (label_Path.Text == "")
            {
                const string message = "Please browes for a file..";
                const string caption = "Open File";
                var          result  = MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else
            {
                if (comboBox1.SelectedIndex == 0)
                {
                    System.Security.Cryptography.MD5CryptoServiceProvider oMD5Hasher =
                        new System.Security.Cryptography.MD5CryptoServiceProvider();

                    try
                    {
                        oFileStream     = GetFileStream(filePath);
                        arrbytHashValue = oMD5Hasher.ComputeHash(oFileStream);
                        oFileStream.Close();

                        strHashData = System.BitConverter.ToString(arrbytHashValue);
                        strHashData = strHashData.Replace("-", "");
                        strResult   = strHashData;
                    }
                    catch (System.Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message, "Error!",
                                                             System.Windows.Forms.MessageBoxButtons.OK,
                                                             System.Windows.Forms.MessageBoxIcon.Error,
                                                             System.Windows.Forms.MessageBoxDefaultButton.Button1);
                    }

                    textBox1.Text = strResult;
                }
                else if (comboBox1.SelectedIndex == 1)
                {
                    System.Security.Cryptography.SHA1CryptoServiceProvider oSHA1Hasher =
                        new System.Security.Cryptography.SHA1CryptoServiceProvider();

                    try
                    {
                        oFileStream     = GetFileStream(filePath);
                        arrbytHashValue = oSHA1Hasher.ComputeHash(oFileStream);
                        oFileStream.Close();

                        strHashData = System.BitConverter.ToString(arrbytHashValue);
                        strHashData = strHashData.Replace("-", "");
                        strResult   = strHashData;
                    }
                    catch (System.Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message, "Error!",
                                                             System.Windows.Forms.MessageBoxButtons.OK,
                                                             System.Windows.Forms.MessageBoxIcon.Error,
                                                             System.Windows.Forms.MessageBoxDefaultButton.Button1);
                    }
                    textBox1.Text = strResult;
                }
                else if (comboBox1.SelectedIndex == 2)
                {
                    System.Security.Cryptography.SHA256CryptoServiceProvider oSHA256Hasher =
                        new System.Security.Cryptography.SHA256CryptoServiceProvider();

                    try
                    {
                        oFileStream     = GetFileStream(filePath);
                        arrbytHashValue = oSHA256Hasher.ComputeHash(oFileStream);
                        oFileStream.Close();

                        strHashData = System.BitConverter.ToString(arrbytHashValue);
                        strHashData = strHashData.Replace("-", "");
                        strResult   = strHashData;
                    }
                    catch (System.Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message, "Error!",
                                                             System.Windows.Forms.MessageBoxButtons.OK,
                                                             System.Windows.Forms.MessageBoxIcon.Error,
                                                             System.Windows.Forms.MessageBoxDefaultButton.Button1);
                    }
                    textBox1.Text = strResult;
                }
                else if (comboBox1.SelectedIndex == 3)
                {
                    System.Security.Cryptography.SHA384CryptoServiceProvider oSHA384Hasher =
                        new System.Security.Cryptography.SHA384CryptoServiceProvider();

                    try
                    {
                        oFileStream     = GetFileStream(filePath);
                        arrbytHashValue = oSHA384Hasher.ComputeHash(oFileStream);
                        oFileStream.Close();

                        strHashData = System.BitConverter.ToString(arrbytHashValue);
                        strHashData = strHashData.Replace("-", "");
                        strResult   = strHashData;
                    }
                    catch (System.Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message, "Error!",
                                                             System.Windows.Forms.MessageBoxButtons.OK,
                                                             System.Windows.Forms.MessageBoxIcon.Error,
                                                             System.Windows.Forms.MessageBoxDefaultButton.Button1);
                    }
                    textBox1.Text = strResult;
                }
                else if (comboBox1.SelectedIndex == 4)
                {
                    System.Security.Cryptography.SHA512CryptoServiceProvider oSHA512Hasher =
                        new System.Security.Cryptography.SHA512CryptoServiceProvider();

                    try
                    {
                        oFileStream     = GetFileStream(filePath);
                        arrbytHashValue = oSHA512Hasher.ComputeHash(oFileStream);
                        oFileStream.Close();

                        strHashData = System.BitConverter.ToString(arrbytHashValue);
                        strHashData = strHashData.Replace("-", "");
                        strResult   = strHashData;
                    }
                    catch (System.Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message, "Error!",
                                                             System.Windows.Forms.MessageBoxButtons.OK,
                                                             System.Windows.Forms.MessageBoxIcon.Error,
                                                             System.Windows.Forms.MessageBoxDefaultButton.Button1);
                    }
                    textBox1.Text = strResult;
                }
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Hash of string as byte array.
 /// </summary>
 /// <param name="inString">Input string</param>
 /// <returns>Output bytes</returns>
 public byte[] hashString(string inString)
 {
     byte[] toHash = System.Text.Encoding.ASCII.GetBytes(inString);
     return(csp.ComputeHash(toHash));
 }
Ejemplo n.º 4
0
        /// <summary>
        /// 计算SHA-384码
        /// </summary>
        /// <param name="word">字符串</param>
        /// <param name="toUpper">返回哈希值格式 true:英文大写,false:英文小写</param>
        /// <returns></returns>
        public static string Hash_SHA_384(string word, bool toUpper = true)
        {
            try
            {
                System.Security.Cryptography.SHA384CryptoServiceProvider SHA384CSP
                    = new System.Security.Cryptography.SHA384CryptoServiceProvider();

                byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word);
                byte[] bytHash = SHA384CSP.ComputeHash(bytValue);
                SHA384CSP.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);
            }
        }