Advertisement
Gireada

SHA-1/MD5/MAC NetBeans

Apr 26th, 2018
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. //SHA-1
  2. private static String byteArrToString(byte[] b){
  3. String res = null;
  4. StringBuffer sb = new StringBuffer(b.length * 2);
  5. for (int i = 0; i < b.length; i++){
  6. int j = b[i] & 0xff;
  7. if (j < 16) {
  8. sb.append('0');
  9. }
  10. sb.append(Integer.toHexString(j));
  11. }
  12. res = sb.toString();
  13. return res.toUpperCase();
  14. }
  15.  
  16. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  17. String passwordSHA = null;
  18. MessageDigest sha = null;
  19. try {
  20. sha = MessageDigest.getInstance("SHA-1");
  21. } catch (NoSuchAlgorithmException ex) {
  22. Logger.getLogger(SHA.class.getName()).log(Level.SEVERE, null, ex);
  23. }
  24. byte[] tmp = jTextArea1.getText().getBytes();
  25. sha.update(tmp);
  26. String textSHA = byteArrToString(sha.digest());
  27. jTextField1.setText(textSHA);
  28. }
  29.  
  30.  
  31. //MD5
  32. public static String toHex(byte [] digest) {
  33. StringBuffer hexString = new StringBuffer();
  34. for(int i = 0; i<digest.length; i++)
  35. {
  36. hexString.append(Integer.toHexString(0xFF & digest[i]));
  37. }
  38. return new String(hexString);
  39. }
  40.  
  41.  
  42. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  43.  
  44. MessageDigest messageDigest = null;
  45. try {
  46. messageDigest = MessageDigest.getInstance("MD5");
  47. } catch (NoSuchAlgorithmException ex) {
  48. Logger.getLogger(MD5.class.getName()).log(Level.SEVERE, null, ex);
  49. }
  50. String text;
  51. text = jTextArea1.getText();
  52. byte[] plaintext = null;
  53. try {
  54. plaintext = text.getBytes("UTF8");
  55. } catch (UnsupportedEncodingException ex) {
  56. Logger.getLogger(MD5.class.getName()).log(Level.SEVERE, null, ex);
  57. }
  58. messageDigest.update(plaintext);
  59. byte[] msgDigest = messageDigest.digest();
  60. String digest = toHex(msgDigest);
  61. jTextField1.setText(digest);
  62. }
  63.  
  64. //MAC
  65.  
  66. public static String toHex(byte [] digest) {
  67. StringBuffer hexString = new StringBuffer();
  68. for(int i = 0; i<digest.length; i++)
  69. {
  70. hexString.append(Integer.toHexString(0xFF & digest[i]));
  71. }
  72. return new String(hexString);
  73. }
  74.  
  75. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  76. KeyGenerator keyGen = null;
  77. try {
  78. keyGen = KeyGenerator.getInstance("HmacMD5");
  79. } catch (NoSuchAlgorithmException ex) {
  80. Logger.getLogger(MAC.class.getName()).log(Level.SEVERE, null, ex);
  81. }
  82. SecretKey MD5key = keyGen.generateKey();
  83.  
  84. String text;
  85. text = jTextArea1.getText();
  86. byte[] plaintext = null;
  87.  
  88. try {
  89. plaintext = text.getBytes("UTF8");
  90. } catch (UnsupportedEncodingException ex) {
  91. Logger.getLogger(MD5.class.getName()).log(Level.SEVERE, null, ex);
  92. }
  93. Mac mac = null;
  94. try {
  95. mac = Mac.getInstance("HmacMD5");
  96. } catch (NoSuchAlgorithmException ex) {
  97. Logger.getLogger(MAC.class.getName()).log(Level.SEVERE, null, ex);
  98. }
  99. try {
  100. mac.init(MD5key);
  101. } catch (InvalidKeyException ex) {
  102. Logger.getLogger(MAC.class.getName()).log(Level.SEVERE, null, ex);
  103. }
  104. mac.update(plaintext);
  105.  
  106. byte[] byteMacFinal = mac.doFinal();
  107. String macFinal = toHex(byteMacFinal);
  108. jTextField1.setText(macFinal);
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement