Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how would I perform a SHA1 hash on a file?

Tags:

c#

sha1

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?

I think this is what GIT does, so I just want to learn how to do it

like image 946
mrblah Avatar asked Dec 28 '09 15:12

mrblah


1 Answers

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.

like image 86
Ta01 Avatar answered Oct 04 '22 23:10

Ta01