Reputation: 73
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.
Upvotes: 5
Views: 7565
Reputation: 5236
The easiest way to do this is Get-FileHash which has been available since PS 4.0
$ echo "Hello world" >1.tm
$
$ Get-FileHash 1.tm
Algorithm Hash
Path
--------- ---- ----
SHA256 B995A2FAFFB3F27C632CE037BDB2ADB04EC3E578D0FA794BA50CFC16F0F219BF
C:\scratch\1.tm
$
Upvotes: 0
Reputation: 4678
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));
}
}
Upvotes: 9