如何在 C# 中执行 SHA1 文件校验和?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1993903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:29:53  来源:igfitidea点击:

How do I do a SHA1 File Checksum in C#?

c#.netsha1checksum

提问by Firesteel

How do I use the SHA1CryptoServiceProvider()on a file to create a SHA1 Checksum of the file?

如何使用SHA1CryptoServiceProvider()文件上的 来创建文件的 SHA1 校验和?

回答by dreadwail

With the ComputeHash method. See here:

使用 ComputeHash 方法。看这里:

ComputeHash

计算哈希

Example snippet:

示例片段:

using(var cryptoProvider = new SHA1CryptoServiceProvider())
{
    string hash = BitConverter
            .ToString(cryptoProvider.ComputeHash(buffer));

    //do something with hash
}

Where buffer is the contents of your file.

其中 buffer 是文件的内容。

回答by dreadwail

using (FileStream fs = new FileStream(@"C:\file\location", FileMode.Open))
using (BufferedStream bs = new BufferedStream(fs))
{
    using (SHA1Managed sha1 = new SHA1Managed())
    {
        byte[] hash = sha1.ComputeHash(bs);
        StringBuilder formatted = new StringBuilder(2 * hash.Length);
        foreach (byte b in hash)
        {
            formatted.AppendFormat("{0:X2}", b);
        }
    }
}

formattedcontains the string representation of the SHA-1 hash. Also, by using a FileStreaminstead of a byte buffer, ComputeHashcomputes the hash in chunks, so you don't have to load the entire file in one go, which is helpful for large files.

formatted包含 SHA-1 哈希的字符串表示。此外,通过使用 aFileStream而不是字节缓冲区,ComputeHash以块为单位计算散列,因此您不必一次性加载整个文件,这对大文件很有帮助。

回答by Daniel James Bryars

If you are already reading the file as a stream, then the following technique calculates the hash as you read it. The only caveat is that you need to consume the whole stream.

如果您已经将文件作为流读取,则以下技术会在您读取文件时计算散列。唯一的警告是您需要消耗整个流。

class Program
    {
        static void Main(string[] args)
        {
            String sourceFileName = "C:\test.txt";
            Byte[] shaHash;

            //Use Sha1Managed if you really want sha1
            using (var shaForStream = new SHA256Managed())
            using (Stream sourceFileStream = File.Open(sourceFileName, FileMode.Open))
            using (Stream sourceStream = new CryptoStream(sourceFileStream, shaForStream, CryptoStreamMode.Read))
            {
                //Do something with the sourceStream 
                //NOTE You need to read all the bytes, otherwise you'll get an exception ({"Hash must be finalized before the hash value is retrieved."}) 
                while(sourceStream.ReadByte() != -1);                
                shaHash = shaForStream.Hash;
            }

            Console.WriteLine(Convert.ToBase64String(shaHash));
        }
    }

回答by SNMetamorph

Also you can try:

您也可以尝试:

FileStream fop = File.OpenRead(@"C:\test.bin");
string chksum = BitConverter.ToString(System.Security.Cryptography.SHA1.Create().ComputeHash(fop));