how would I perform a SHA1 hash on a file?

后端 未结 2 382
囚心锁ツ
囚心锁ツ 2021-01-03 02:04

If I have a file that I want to monitor for any changes (other than looking at the file date stamps etc).

How could I perform a SHA1 hash on its contents?

2条回答
  •  星月不相逢
    2021-01-03 03:01

    using (FileStream stream = File.OpenRead(@"C:\File.ext"))
    {
        using (SHA1Managed sha = new SHA1Managed())
        {
            byte[] checksum = sha.ComputeHash(stream);
            string sendCheckSum = BitConverter.ToString(checksum)
                .Replace("-", string.Empty);
        }
    }
    

    Calculate the checksum periodically.

提交回复
热议问题