Exemple #1
0
        /// <summary>
        /// 转换成RSA1散列字符串
        /// </summary>
        /// <param name="source">源数据流</param>
        /// <param name="toLower">返回哈希值格式 true:英文小写,false:英文大写</param>
        /// <returns>RSA1散列字符串</returns>
        /// <example>
        /// file.ToRsa1Hash();
        /// </example>
        public static string ToRsa1Hash(this Stream source, bool toLower = true)
        {
            var result = string.Empty;

            using (var hash = new System.Security.Cryptography.SHA1CryptoServiceProvider())
            {
                hash.Initialize();
                var bytes = hash.ComputeHash(source);
                result = Convert.ToBase64String(bytes);
            }

            return(toLower ? result.ToLower() : result.ToUpper());
        }
Exemple #2
0
        private void buttonMD5_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream(textBoxFileName.Text, FileMode.Open);

            System.Security.Cryptography.MD5CryptoServiceProvider  md5  = new System.Security.Cryptography.MD5CryptoServiceProvider();
            System.Security.Cryptography.SHA1CryptoServiceProvider sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider();
            md5.Initialize();
            sha1.Initialize();
            byte[] md5Hash = md5.ComputeHash(fs);
            fs.Seek(0, SeekOrigin.Begin);
            byte[] sha1Hash = sha1.ComputeHash(fs);
            fs.Close();
            string sMD5  = "";
            string sSHA1 = "";

            for (int i = 0; i < md5Hash.Length; i++)
            {
                sMD5 += string.Format("{0:X2}", md5Hash[i]);
            }
            for (int i = 0; i < sha1Hash.Length; i++)
            {
                sSHA1 += string.Format("{0:X2}", sha1Hash[i]);
            }
            if (sSHA1.ToUpper() == textBoxSHA1Compare.Text.ToUpper())
            {
                textBoxSHA1Compare.ForeColor = Color.Green;
            }
            else
            {
                textBoxSHA1Compare.ForeColor = Color.Red;
            }
            if (sMD5.ToUpper() == textBoxMD5Compare.Text.ToUpper())
            {
                textBoxMD5Compare.ForeColor = Color.Green;
            }
            else
            {
                textBoxMD5Compare.ForeColor = Color.Red;
            }
            textBoxMD5Compare.Text  = textBoxMD5Compare.Text.ToUpper();
            textBoxSHA1Compare.Text = textBoxSHA1Compare.Text.ToUpper();
            textBoxCalMd5.Text      = sMD5.ToUpper();
            textBoxSHACalc.Text     = sSHA1.ToUpper();
        }