Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. package laudarch.utils;
  2.  
  3. import java.io.IOException;
  4. import java.security.MessageDigest;
  5. import java.security.NoSuchAlgorithmException;
  6.  
  7. /**
  8. * Created by laudarch on 16/12/13.
  9. */
  10.  
  11. public class EncryptionUtils {
  12. public static String sha1(String input) throws NoSuchAlgorithmException {
  13. MessageDigest mDigest = MessageDigest.getInstance("SHA1");
  14. byte[] result = mDigest.digest(input.getBytes());
  15. StringBuffer sb = new StringBuffer();
  16.  
  17. for (int i = 0; i < result.length; i++) {
  18. sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
  19. }
  20.  
  21. return sb.toString();
  22. }
  23.  
  24. public static String sha256(String input) throws NoSuchAlgorithmException {
  25. MessageDigest mDigest = MessageDigest.getInstance("SHA256");
  26. byte[] result = mDigest.digest(input.getBytes());
  27. StringBuffer sb = new StringBuffer();
  28.  
  29. for (int i = 0; i < result.length; i++) {
  30. sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
  31. }
  32.  
  33. return sb.toString();
  34. }
  35.  
  36.  
  37. /**
  38. * Verifies SHA1 checksum
  39. * @param data to be verified
  40. * @param testChecksum the expected checksum
  41. * @return true if the SHA1 checksum of data matches the given SHA1 checksum; false otherwise.
  42. * @throws NoSuchAlgorithmException
  43. * @throws IOException
  44. */
  45. public static boolean verifySHA1Checksum(String data, String testChecksum) throws NoSuchAlgorithmException, IOException
  46. {
  47. String dataHash = EncryptionUtils.sha1(data);
  48.  
  49. return dataHash.equals(testChecksum);
  50. }
  51.  
  52. /**
  53. * Verifies SHA256 checksum
  54. * @param data to be verified
  55. * @param testChecksum the expected checksum
  56. * @return true if the SHA256 checksum of the data matches the given SHA256 checksum; false otherwise.
  57. * @throws NoSuchAlgorithmException
  58. * @throws IOException
  59. */
  60. public static boolean verifyChecksum(String data, String testChecksum) throws NoSuchAlgorithmException, IOException
  61. {
  62. String dataHash = sha256(data);
  63.  
  64. return dataHash.equals(testChecksum);
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement