Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- C# code using the API to produce a message digest from a password
- using System;
- using System.Text;
- using System.Security.Cryptography;
- public class TestSHA1
- {
- public static void Main()
- {
- string message = "abc";
- SHA1 sha = new SHA1Managed();
- ASCIIEncoding ae = new ASCIIEncoding();
- byte[] data = ae.GetBytes(message);
- byte[] digest = sha.ComputeHash(data);
- Console.WriteLine(GetAsString(digest));
- Console.ReadLine();
- }
- public static string GetAsString(byte[] bytes)
- {
- StringBuilder s = new StringBuilder();
- int length = bytes.Length;
- for (int n=0; n < length; n++)
- {
- s.Append((int) bytes[n]);
- if (n != length - 1) { s.Append(' '); }
- }
- return s.ToString();
- }
- }
- The C# code produces the following message digest for the string "abc"
- 169 153 62 54 71 6 129 106 186 62 37 113 120 80 194 108 156 208 216 157
- C# code to convert to hexadecimal string
- public static string GetAsHexaDecimal(byte[] bytes)
- {
- StringBuilder s = new StringBuilder();
- int length = bytes.Length;
- for (int n=0; n < length; n++)
- {
- s.Append(String.Format("{0,2:x}", bytes[n]).Replace(" ", "0"));
- }
- return s.ToString();
- }
- Produces the hash
- a9993e364706816aba3e25717850c26c9cd0d89d
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement