I want to hash given byte[]
array with using SHA1
Algorithm with the use of SHA1Managed
.
The byte[]
hash will come from unit test.
Expected hash is 0d71ee4472658cd5874c5578410a9d8611fc9aef
(case sensitive).
How can I achieve this?
public string Hash(byte [] temp)
{
using (SHA1Managed sha1 = new SHA1Managed())
{
}
}
I'll throw my hat in here:
(as part of a static class, as this snippet is two extensions)
You can "compute the value for the specified byte array" using
ComputeHash
:If you want to analyse the result in string representation, then you will need to format the bytes using the
{0:X2}
format specifier.EDIT:
You could also specify the encoding when converting the byte array to string as follows:
or
For those who want a "standard" text formatting of the hash, you can use something like the following:
This will produce a hash like
0C2E99D0949684278C30B9369B82638E1CEAD415
.Or for a code golfed version:
This is what I went with. For those of you who want to optimize, check out https://stackoverflow.com/a/624379/991863.
Fastest way is this :
For Small character output use
x2
in replace of ofX2