PasswordEncoder.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package com.miniframe.bisiness.service;
  2. import java.math.BigInteger;
  3. import java.security.MessageDigest;
  4. import javax.crypto.KeyGenerator;
  5. import javax.crypto.Mac;
  6. import javax.crypto.SecretKey;
  7. import javax.crypto.spec.SecretKeySpec;
  8. import org.apache.commons.codec.binary.Base64;
  9. /**
  10. * 基础加密组件
  11. *
  12. * @author tianyubing
  13. * @version 1.0
  14. * @since 1.0
  15. */
  16. public abstract class PasswordEncoder {
  17. public static final String KEY_SHA = "SHA";
  18. public static final String KEY_MD5 = "MD5";
  19. /**
  20. * MAC算法可选以下多种算法
  21. *
  22. * <pre>
  23. * HmacMD5
  24. * HmacSHA1
  25. * HmacSHA256
  26. * HmacSHA384
  27. * HmacSHA512
  28. * </pre>
  29. */
  30. public static final String KEY_MAC = "HmacSHA512";
  31. /**
  32. * passwordEncoder加密
  33. *
  34. * @param data
  35. * @param key
  36. * @return
  37. * @throws Exception
  38. */
  39. public static String passwordEncoder(String inString, String key) throws Exception {
  40. BigInteger mac = new BigInteger(encryptHMAC(inString.getBytes(), key));
  41. return mac.toString(16);
  42. }
  43. /**
  44. * BASE64解密
  45. *
  46. * @param key
  47. * @return
  48. * @throws Exception
  49. */
  50. public static byte[] decryptBASE64(String key) throws Exception {
  51. return Base64.decodeBase64(key);
  52. }
  53. /**
  54. * BASE64加密
  55. *
  56. * @param key
  57. * @return
  58. * @throws Exception
  59. */
  60. public static String encryptBASE64(byte[] key) throws Exception {
  61. return Base64.encodeBase64String(key);
  62. }
  63. /**
  64. * MD5加密
  65. *
  66. * @param data
  67. * @return
  68. * @throws Exception
  69. */
  70. public static byte[] encryptMD5(byte[] data) throws Exception {
  71. MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
  72. md5.update(data);
  73. return md5.digest();
  74. }
  75. /**
  76. * SHA加密
  77. *
  78. * @param data
  79. * @return
  80. * @throws Exception
  81. */
  82. public static byte[] encryptSHA(byte[] data) throws Exception {
  83. MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
  84. sha.update(data);
  85. return sha.digest();
  86. }
  87. /**
  88. * 初始化HMAC密钥
  89. *
  90. * @return
  91. * @throws Exception
  92. */
  93. public static String initMacKey() throws Exception {
  94. KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);
  95. SecretKey secretKey = keyGenerator.generateKey();
  96. return encryptBASE64(secretKey.getEncoded());
  97. }
  98. /**
  99. * HMAC加密
  100. *
  101. * @param data
  102. * @param key
  103. * @return
  104. * @throws Exception
  105. */
  106. public static byte[] encryptHMAC(byte[] data, String key) throws Exception {
  107. SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), KEY_MAC);
  108. Mac mac = Mac.getInstance(secretKey.getAlgorithm());
  109. mac.init(secretKey);
  110. return mac.doFinal(data);
  111. }
  112. public static boolean assertEquals(String code, String code1) {
  113. if (code.equals(code1)) {
  114. return true;
  115. } else {
  116. return false;
  117. }
  118. }
  119. public static boolean assertArrayEquals(byte[] code, byte[] code1) {
  120. if (code == code1) {
  121. return true;
  122. } else {
  123. return false;
  124. }
  125. }
  126. }