Ejemplo n.º 1
0
        // IDisposable methods

        /// <include file='doc\HMACSHA1.uex' path='docs/doc[@for="HMACSHA1.Dispose"]/*' />
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                // We always want to dispose _hash1
                if (_hash1 != null)
                {
                    _hash1.Clear();
                }
                // We always want to dispose _hash2
                if (_hash2 != null)
                {
                    _hash2.Clear();
                }
                // We want to zeroize _rgbInner and _rgbOuter
                if (_rgbInner != null)
                {
                    Array.Clear(_rgbInner, 0, _rgbInner.Length);
                }
                if (_rgbOuter != null)
                {
                    Array.Clear(_rgbOuter, 0, _rgbOuter.Length);
                }
            }
            // call the base class's Dispose
            base.Dispose(disposing);
        }
Ejemplo n.º 2
0
	}//ComputeCRC32


	/// <summary>
	/// 获取文件的SHA1
	/// </summary>
	/// <param name="fileName"></param>
	/// <returns></returns>
	public static String GetSHA1(String fileName)
	{
		String hashSHA1 = String.Empty;
		//检查文件是否存在,如果文件存在则进行计算,否则返回空值
		if (File.Exists(fileName))
		{
			using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
			{
				//计算文件的SHA1值
				System.Security.Cryptography.SHA1 calculator = System.Security.Cryptography.SHA1.Create();
				Byte[] buffer = calculator.ComputeHash(fileStream);
				calculator.Clear();
				//将字节数组转换成十六进制的字符串形式
				StringBuilder stringBuilder = new StringBuilder();
				for (int bufferIdx = 0; bufferIdx < buffer.Length; bufferIdx++)
				{
					stringBuilder.Append(buffer[bufferIdx].ToString("x2"));
				}
				hashSHA1 = stringBuilder.ToString();

			}//关闭文件流
		}
		else
		{
			Console.Error.WriteLine("{0}文件找不到!", fileName);
		}
		return hashSHA1;
	}//end GetSHA1
Ejemplo n.º 3
0
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="plainText">明文</param>
        /// <param name="encryptKey">加密密钥(加密密钥为null)</param>
        /// <returns></returns>
        public string Encrypt(string plainText, string encryptKey)
        {
            //将明文转为字节数组
            byte[] arrPlainText = Encoding.UTF8.GetBytes(plainText);
            //计算字节数组的哈希值
            byte[] arrHash = mCrypto.ComputeHash(arrPlainText);
            mCrypto.Clear();

            StringBuilder result = new StringBuilder();

            for (int i = 0; i < arrHash.Length; i++)
            {
                result.Append(arrHash[i].ToString("x2"));
            }
            return(result.ToString());
        }
Ejemplo n.º 4
0
        public static string Encrypt_Sha1_16(byte[] bfs)
        {
            String hashSHA1 = String.Empty;

            System.Security.Cryptography.SHA1 calculator = System.Security.Cryptography.SHA1.Create();
            Byte[] buffer = calculator.ComputeHash(bfs);
            calculator.Clear();
            //将字节数组转换成十六进制的字符串形式
            StringBuilder stringBuilder = new StringBuilder();

            for (int i = 0; i < buffer.Length; i++)
            {
                stringBuilder.Append(buffer[i].ToString("x2"));
            }
            hashSHA1 = stringBuilder.ToString();

            return(hashSHA1);
        }
Ejemplo n.º 5
0
        public static List <string> ComputeSHA1ByParts(string fileName, int partSize)
        {
            List <string> hashList = null;

            if (System.IO.File.Exists(fileName))
            {
                using (System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read))
                {
                    System.Security.Cryptography.SHA1 calculator = System.Security.Cryptography.SHA1.Create();

                    long nSize = fs.Length;

                    hashList = new List <string>();
                    int           bufSize = partSize * 1024 * 1024;
                    byte[]        buffer  = new byte[bufSize];
                    byte[]        hashBuf;
                    int           nLen     = 0;
                    int           nPos     = 0;
                    int           nCount   = 0;
                    StringBuilder strBuild = new StringBuilder();

                    do
                    {
                        nLen  = fs.Read(buffer, 0, bufSize);
                        nPos += nLen;

                        hashBuf = calculator.ComputeHash(buffer);
                        strBuild.Clear();
                        foreach (var item in hashBuf)
                        {
                            strBuild.Append(item.ToString("x2"));
                        }
                        hashList.Add(strBuild.ToString());
                        nCount++;
                    } while (nPos != nSize);

                    calculator.Clear();
                    //hashSHA1 = stringBuilder.ToString();
                }
            }

            return(hashList);
        }
Ejemplo n.º 6
0
    /// <summary>
    /// 计算指定文件的SHA1值
    /// </summary>
    public static string SHA1(string fileName)
    {
        string hashSHA1 = string.Empty;

        if (File.Exists(fileName))
        {
            using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                System.Security.Cryptography.SHA1 calculator = System.Security.Cryptography.SHA1.Create();
                byte[] buffer = calculator.ComputeHash(fs);
                calculator.Clear();
                System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
                for (int i = 0; i < buffer.Length; i++)
                {
                    stringBuilder.Append(buffer[i].ToString("x2"));
                }
                hashSHA1 = stringBuilder.ToString();
            }
        }
        return(hashSHA1);
    }
Ejemplo n.º 7
0
        public static String ComputeSHA1(String fileName)
        {
            String hashSHA1 = String.Empty;

            //检查文件是否存在,如果文件存在则进行计算,否则返回空值
            if (System.IO.File.Exists(fileName))
            {
                using (System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
                {
                    //计算文件的SHA1值
                    System.Security.Cryptography.SHA1 calculator = System.Security.Cryptography.SHA1.Create();
                    Byte[] buffer = calculator.ComputeHash(fs);
                    calculator.Clear();
                    //将字节数组转换成十六进制的字符串形式
                    StringBuilder stringBuilder = new StringBuilder();
                    for (int i = 0; i < buffer.Length; i++)
                    {
                        stringBuilder.Append(buffer[i].ToString("x2"));
                    }
                    hashSHA1 = stringBuilder.ToString();
                }//关闭文件流
            }
            return(hashSHA1);
        }