Exemplo n.º 1
0
        private void btnOK_Click(object sender, EventArgs e)
        {
            HashAlgorithmType hashAlgorithmType = HashAlgorithmType.MD5;

            if (rbMD5.Checked)
            {
                hashAlgorithmType = HashAlgorithmType.MD5;
            }
            if (rbSHA1.Checked)
            {
                hashAlgorithmType = HashAlgorithmType.SHA1;
            }
            if (rbSHA256.Checked)
            {
                hashAlgorithmType = HashAlgorithmType.SHA256;
            }
            if (rbSHA384.Checked)
            {
                hashAlgorithmType = HashAlgorithmType.SHA384;
            }
            if (rbSHA512.Checked)
            {
                hashAlgorithmType = HashAlgorithmType.SHA512;
            }

            if (SC != null && SC.S1.Length > 0)
            {
                if (cbFileHash.Checked)
                {
                    if (File.Exists(SC.S1))
                    {
                        this.Cursor = Cursors.WaitCursor;
                        FileStream fs = new FileStream(SC.S1, FileMode.Open, FileAccess.Read, FileShare.Read, 1024 * 1024);
                        SC.S2       = HashCryto.GetHash2String(fs, hashAlgorithmType);
                        this.Cursor = Cursors.Default;
                    }
                    else
                    {
                        MsgBox.Show(string.Format("文件{0}不存在", SC.S1));
                    }
                    return;
                }

                if (cbBase64.Checked)
                {
                    SC.S2 = HashCryto.GetHash2Base64(SC.S1, hashAlgorithmType, SC.GetEncoding());
                }
                else
                {
                    SC.S2 = HashCryto.GetHash2String(SC.S1, hashAlgorithmType, SC.GetEncoding());
                }
            }
            this.Close();
        }
Exemplo n.º 2
0
        /// <summary>
        /// 计算签名
        /// </summary>
        /// <param name="nvc">该集合保存签名相关参数的值</param>
        /// <param name="secretKey">签名密钥</param>
        /// <returns></returns>
        public string Sign(NameValueCollection nvc, string secretKey)
        {
            StringBuilder sb = new StringBuilder();

            foreach (KeyValuePair <string, string> temp in nvc)
            {
                sb.Append(temp.Value);
            }

            sb.Append(secretKey);

            string s = HashCryto.GetHash2String(sb.ToString(), _HashAlgorithmType, _Encoding);

            return(s);
        }
Exemplo n.º 3
0
        private string SignUrl(string baseUrl, NameValueCollection urlParams, IEnumerable <string> allKeys, string key)
        {
            StringBuilder urlBuilder = new StringBuilder(baseUrl + "?");
            //创建待加密字符串
            StringBuilder paramString = new StringBuilder();

            foreach (var k in allKeys)
            {
                paramString.Append(urlParams[k]);
                urlBuilder.Append(string.Format("{0}={1}&", k, urlParams[k]));
            }

            paramString.Append(key);

            //加密
            string signedString = HashCryto.GetHash2String(paramString.ToString(), _HashAlgorithmType, _Encoding);

            urlBuilder.Append(string.Format("{0}={1}", "sign", signedString));

            return(urlBuilder.ToString());
        }
Exemplo n.º 4
0
 /// <summary>
 /// 返回指定加密哈希算法的字符串的副本
 /// </summary>
 /// <param name="s"></param>
 /// <param name="hashAlgorithm">哈希算法</param>
 /// <returns></returns>
 public static string ToHash(string s, HashAlgorithmType hashAlgorithm)
 {
     return(HashCryto.GetHash2String(s, hashAlgorithm));
 }