Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. public static String shaHash(String input)
  2. throws NoSuchAlgorithmException {
  3. if (input == null) {
  4. return null;
  5. }
  6. MessageDigest sha = MessageDigest.getInstance("SHA");
  7. try {
  8. sha.update(input.getBytes("UTF8"));
  9. BigInteger hash = new BigInteger(1, sha.digest());
  10. return hash.toString(16);
  11. } catch (UnsupportedEncodingException e) {}
  12. return null;
  13. }
  14.  
  15. StringBuilder convertedId = new StringBuilder();
  16. var idToByte = Encoding.ASCII.GetBytes(id);
  17. using (SHA1 sha1 = SHA1.Create())
  18. {
  19. var bytes = sha1.ComputeHash(idToByte);
  20. for (int i = 0; i < bytes.Length; i++)
  21. {
  22. convertedId.Append(bytes[i].ToString("x2"));
  23. }
  24. }
  25. return convertedId.ToString();
  26.  
  27. StringBuilder convertedId = new StringBuilder();
  28. var idToByte = Encoding.ASCII.GetBytes(id);
  29. using (SHA1 sha1 = SHA1.Create())
  30. {
  31. var bytes = sha1.ComputeHash(idToByte);
  32. for (int i = 0; i < bytes.Length; i++)
  33. {
  34. if (i == 0 && bytes[i] < 16)
  35. convertedId.Append(bytes[i].ToString("x1"));
  36. else
  37. convertedId.Append(bytes[i].ToString("x2"));
  38. }
  39. }
  40. return convertedId.ToString();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement