Exemplo n.º 1
0
        /// <summary>
        ///  计算指定文件的SHA1值
        /// </summary>
        /// <param name="fileName">指定文件的完全限定名称</param>
        /// <returns>返回值的字符串形式</returns>
        public static String ComputeSHA1(String fileName)
        {
            String hashSHA1 = String.Empty;

            try
            {
                //检查文件是否存在,如果文件存在则进行计算,否则返回空值
                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();
                    }//关闭文件流
                }
            }
            catch (Exception ex)
            { }
            return(hashSHA1);
        }
Exemplo 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
Exemplo n.º 3
0
        public static string ComputeSHA1(string path)
        {
            if (!File.Exists(path))
            {
                throw new NotFileException(path, nameof(path));
            }

            string hashSHA1 = string.Empty;

            //检查文件是否存在,如果文件存在则进行计算,否则返回空值

            using (System.IO.FileStream fs = new System.IO.FileStream(path, 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);
        }
Exemplo n.º 4
0
 public string GetHash(string filename)
 {
     using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
     {
         System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create();
         byte[] bs = sha1.ComputeHash(fs);
         sha1.Clear();
         string h = BitConverter.ToString(bs).ToLower().Replace("-", "");
         return($"{h}_{Model}_{PitchShift:0.0000}_{FormantShift}");
     }
 }
Exemplo n.º 5
0
        }//ComputeCRC32

        /// <summary>
        ///  计算指定文件的SHA1值
        /// </summary>
        /// <param name="fs">文件流</param>
        /// <returns>返回值的字符串形式</returns>
        public static Byte[] SHA1(FileStream fs)
        {
            String hashSHA1 = String.Empty;

            //检查文件是否存在,如果文件存在则进行计算,否则返回空值

            //计算文件的SHA1值
            System.Security.Cryptography.SHA1 calculator = System.Security.Cryptography.SHA1.Create();
            Byte[] buffer = calculator.ComputeHash(fs);
            calculator.Clear();
            return(buffer);
        }
Exemplo n.º 6
0
        }//ComputeMD5

        /// <summary>
        ///  计算指定文件的SHA1值
        /// </summary>
        /// <param name="fileName">指定文件的完全限定名称</param>
        /// <returns>返回值的字符串形式</returns>
        public static String ComputeSHA1(Stream stream)
        {
            String hashSHA1 = String.Empty;

            //计算文件的SHA1值
            System.Security.Cryptography.SHA1 calculator = System.Security.Cryptography.SHA1.Create();
            Byte[] buffer = calculator.ComputeHash(stream);
            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);
        }
Exemplo n.º 7
0
        }    //ComputeSHA1

        public static string ComputeSha1(System.IO.Stream stream)
        {
            string _result = "";

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

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

            return(_result);
        }
Exemplo n.º 8
0
        private static string GetFileShaHash(string filePath)
        {
            string hashSHA1 = String.Empty;

            if (System.IO.File.Exists(filePath))
            {
                using (System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read))
                {
                    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);
        }
Exemplo n.º 9
0
        /// <summary>
        ///  计算指定Str的SHA1值
        /// </summary>
        /// <param name="str">待计算的字符串</param>
        /// <returns>返回值的字符串形式</returns>
        public static String ComputeSHA1ByStr(string str)
        {
            String hashSHA1 = String.Empty;

            try
            {
                //计算文件的SHA1值
                System.Security.Cryptography.SHA1 calculator = System.Security.Cryptography.SHA1.Create();
                Byte[] buffer = calculator.ComputeHash(System.Text.Encoding.UTF8.GetBytes(str));
                calculator.Clear();
                //将字节数组转换成十六进制的字符串形式
                StringBuilder stringBuilder = new StringBuilder();
                for (int i = 0; i < buffer.Length; i++)
                {
                    stringBuilder.Append(buffer[i].ToString("x2"));
                }
                hashSHA1 = stringBuilder.ToString();
            }
            catch (Exception ex)
            { }
            return(hashSHA1);
        }