Hashing using SHA1 algorithm in C #

I want to hash a byte[] array using the SHA1 Algorithm using SHA1Managed .
The byte[] hash will be obtained from unit test.
The expected hash is 0d71ee4472658cd5874c5578410a9d8611fc9aef (case sensitive).

How can I achieve this?

 public string Hash(byte [] temp) { using (SHA1Managed sha1 = new SHA1Managed()) { } } 
+60
hashcode c # byte hash sha1
Jun 25 '13 at 8:17
source share
7 answers

For those who want β€œstandard” hash text formatting, you can use something like the following:

 static string Hash(string input) { using (SHA1Managed sha1 = new SHA1Managed()) { var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(input)); var sb = new StringBuilder(hash.Length * 2); foreach (byte b in hash) { // can be "x2" if you want lowercase sb.Append(b.ToString("X2")); } return sb.ToString(); } } 

This will create a hash, e.g. 0C2E99D0949684278C30B9369B82638E1CEAD415 .

Or for golf with the code:

 static string Hash(string input) { var hash = new SHA1Managed().ComputeHash(Encoding.UTF8.GetBytes(input)); return string.Concat(hash.Select(b => b.ToString("x2"))); } 
+130
Oct 25 '14 at 0:26
source share
 public string Hash(byte [] temp) { using (SHA1Managed sha1 = new SHA1Managed()) { var hash = sha1.ComputeHash(temp); return Convert.ToBase64String(hash); } } 

EDIT:

You can also specify the encoding when converting an array of bytes to a string as follows:

 return System.Text.Encoding.UTF8.GetString(hash); 

or

 return System.Text.Encoding.Unicode.GetString(hash); 
+33
Jun 25 '13 at 8:21
source share

This is what I went with. For those of you who want to optimize, check out the https://stackoverflow.com/a/167148/ .

  public static string Hash(string stringToHash) { using (var sha1 = new SHA1Managed()) { return BitConverter.ToString(sha1.ComputeHash(Encoding.UTF8.GetBytes(stringToHash))); } } 
+10
Dec 21 '15 at 16:38
source share

You can "calculate the value for the specified byte array" using ComputeHash :

 var hash = sha1.ComputeHash(temp); 

If you want to parse the result in a string representation, you will need to format the bytes using the format specifier {0:X2} .

+6
Jun 25 '13 at 8:18
source share

The fastest way:

  public static string GetHash(string input) { return string.Join("", (new SHA1Managed().ComputeHash(Encoding.UTF8.GetBytes(input))).Select(x => x.ToString("X2")).ToArray()); } 

For output with a small character, use x2 instead of x2

+5
Aug 14 '16 at 14:22
source share

I will throw my hat here:

(as part of a static class, as this fragment is two extensions)

 //hex encoding of the hash, in uppercase. public static string Sha1Hash (this string str) { byte[] data = UTF8Encoding.UTF8.GetBytes (str); data = data.Sha1Hash (); return BitConverter.ToString (data).Replace ("-", ""); } // Do the actual hashing public static byte[] Sha1Hash (this byte[] data) { using (SHA1Managed sha1 = new SHA1Managed ()) { return sha1.ComputeHash (data); } 
+4
Jun 18 '15 at 23:02
source share

Here is the best version of SHA1 calculation for this line.

 public class ComputeSha1 { public static string SHA1HashStringForUTF8String(string s) { byte[] bytes = Encoding.UTF8.GetBytes(s); var sha1 = SHA1.Create(); byte[] hashBytes = sha1.ComputeHash(bytes); return HexStringFromBytes(hashBytes); } public static string HexStringFromBytes(byte[] bytes) { var sb = new StringBuilder(); foreach (byte b in bytes) { var hex = b.ToString("x2"); sb.Append(hex); } return sb.ToString(); } } // Calling method string result= ComputeSha1.SHA1HashStringForUTF8String("mehran"); 
-one
Jun 27 '19 at 6:23
source share



All Articles