MD5 hash method


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /// <summary>
  2. /// Hash an input string and return the hash as a 32 character hexadecimal string.
  3. /// </summary>
  4. /// <param name="input"></param>
  5. /// <returns></returns>
  6. public string MD5(string input)
  7. {
  8. byte[] textBytes = System.Text.Encoding.Default.GetBytes(input);
  9. try
  10. {
  11. System.Security.Cryptography.MD5CryptoServiceProvider cryptHandler;
  12. cryptHandler = new System.Security.Cryptography.MD5CryptoServiceProvider();
  13. byte[] hash = cryptHandler.ComputeHash(textBytes);
  14. string ret = "";
  15. foreach (byte a in hash)
  16. {
  17. if (a < 16)
  18. ret += "0" + a.ToString("x");
  19. else
  20. ret += a.ToString("x");
  21. }
  22. return ret;
  23. }
  24. catch
  25. {
  26. throw;
  27. }
  28. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.