Advertisement
Guest User

SHA1 HEX DIGEST

a guest
Nov 26th, 2010
1,010
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.23 KB | None | 0 0
  1. C# code using the API to produce a message digest from a password
  2.  
  3. using System;
  4. using System.Text;
  5. using System.Security.Cryptography;
  6.  
  7. public class TestSHA1
  8. {
  9.  public static void Main()
  10.  {
  11.   string message = "abc";
  12.   SHA1 sha = new SHA1Managed();
  13.   ASCIIEncoding ae = new ASCIIEncoding();
  14.   byte[] data = ae.GetBytes(message);
  15.   byte[] digest = sha.ComputeHash(data);
  16.   Console.WriteLine(GetAsString(digest));
  17.   Console.ReadLine();
  18.  }
  19.  public static string GetAsString(byte[] bytes)
  20.  {
  21.   StringBuilder s = new StringBuilder();
  22.   int length = bytes.Length;
  23.   for (int n=0; n < length; n++)
  24.   {
  25.    s.Append((int) bytes[n]);
  26.    if (n != length - 1) { s.Append(' '); }
  27.   }
  28.   return s.ToString();
  29.  }
  30. }
  31.  
  32.  
  33. The C# code produces the following message digest for the string "abc"
  34.  
  35. 169 153 62 54 71 6 129 106 186 62 37 113 120 80 194 108 156 208 216 157
  36.  
  37.  
  38. C# code to convert to hexadecimal string
  39.  
  40. public static string GetAsHexaDecimal(byte[] bytes)
  41. {
  42.  StringBuilder s = new StringBuilder();
  43.  int length = bytes.Length;
  44.  for (int n=0; n < length; n++)
  45.  {
  46.   s.Append(String.Format("{0,2:x}", bytes[n]).Replace(" ", "0"));
  47.  }
  48.  return s.ToString();
  49. }
  50.  
  51.  
  52. Produces the hash
  53. a9993e364706816aba3e25717850c26c9cd0d89d
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement