using (FileStream fs = new FileStream(@"C:filelocation", 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);
}
}
}
formatted
contains the string representation of the SHA-1 hash. Also, by using a FileStream
instead of a byte buffer, ComputeHash
computes the hash in chunks, so you don't have to load the entire file in one go, which is helpful for large files.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…