Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get SHA1 binary base64 hash of a file on C#

I am thinking on creating a program on C# to get the SHA1 binary base64 hash of a series of very large files.

Right now I can accomplish that by running this instruction on OpenSSL:

openssl sha1 -binary FILENAME | openssl base64

however I haven´t found references for the "binary" and "base64" parts to obtain the same result with C#.

Is it possible? Maybe call openssl inside C#?

Thank you.

like image 584
user33177 Avatar asked Oct 03 '13 03:10

user33177


1 Answers

You need to use the Convert.ToBase64String method and the System.IO namespace to read binary:

string GetBase64EncodedSHA1Hash(string filename)
{
    using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
    using (SHA1Managed sha1 = new SHA1Managed())
    {
        return Convert.ToBase64String(sha1.ComputeHash(fs));
    }
}
like image 117
Ashigore Avatar answered Nov 06 '22 12:11

Ashigore