MD5
public static String GetMD5Hash(String input)
{
Encoder enc = Encoding.Unicode.GetEncoder();
byte[] unicodeText = new byte[input.Length * 2];
enc.GetBytes(input.ToCharArray(), 0, input.Length, unicodeText, 0, true);
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(unicodeText);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < result.Length; i++)
{
sb.Append(result[i].ToString("X2"));
}
return sb.ToString();
}[TestMethod]
public void Hash_MD5()
{
String Text = "Hello World!";
String Hashed = HashServices.GetMD5Hash(Text);
Console.WriteLine(Hashed);
}
SHA1
public static String GetSHA1Hash(String input)
{
Encoder enc = Encoding.Unicode.GetEncoder();
byte[] unicodeText = new byte[input.Length * 2];
enc.GetBytes(input.ToCharArray(), 0, input.Length, unicodeText, 0, true);
SHA1 sha1 = new SHA1CryptoServiceProvider();
byte[] result = sha1.ComputeHash(unicodeText);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < result.Length; i++)
{
sb.Append(result[i].ToString("X2"));
}
return sb.ToString();
}[TestMethod]
public void Hash_SHA1()
{
String Text = "Hello World!";
String Hashed = HashServices.GetSHA1Hash(Text);
Console.WriteLine(Hashed);
}
No comments:
Post a Comment