Digests.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package com.happy.common.http;
  2. import org.apache.commons.codec.binary.Hex;
  3. import org.apache.commons.lang3.Validate;
  4. import java.security.GeneralSecurityException;
  5. import java.security.MessageDigest;
  6. import java.security.SecureRandom;
  7. import java.util.Random;
  8. /**
  9. * Http Digest
  10. *
  11. * @author 刘岩松
  12. * @date 2020/6/23 9:37
  13. */
  14. public class Digests {
  15. private static SecureRandom random = new SecureRandom();
  16. /**
  17. * 加密遵循RFC2671规范 将相关参数加密生成一个MD5字符串,并返回
  18. */
  19. public static String http_da_calc_HA1(String username, String realm, String password,
  20. String nonce, String nc, String cnonce, String qop,
  21. String method, String uri, String algorithm) {
  22. String HA1, HA2;
  23. if ("MD5-sess".equals(algorithm)) {
  24. HA1 = HA1_MD5_sess(username, realm, password, nonce, cnonce);
  25. } else {
  26. HA1 = HA1_MD5(username, realm, password);
  27. }
  28. byte[] md5Byte = md5(HA1.getBytes());
  29. HA1 = new String(Hex.encodeHex(md5Byte));
  30. md5Byte = md5(HA2(method, uri).getBytes());
  31. HA2 = new String(Hex.encodeHex(md5Byte));
  32. String original = HA1 + ":" + (nonce + ":" + nc + ":" + cnonce + ":" + qop) + ":" + HA2;
  33. md5Byte = md5(original.getBytes());
  34. return new String(Hex.encodeHex(md5Byte));
  35. }
  36. /**
  37. * algorithm值为MD5时规则
  38. */
  39. private static String HA1_MD5(String username, String realm, String password) {
  40. return username + ":" + realm + ":" + password;
  41. }
  42. /**
  43. * algorithm值为MD5-sess时规则
  44. */
  45. private static String HA1_MD5_sess(String username, String realm, String password, String nonce, String cnonce) {
  46. // MD5(username:realm:password):nonce:cnonce
  47. String s = HA1_MD5(username, realm, password);
  48. byte[] md5Byte = md5(s.getBytes());
  49. String smd5 = new String(Hex.encodeHex(md5Byte));
  50. return smd5 + ":" + nonce + ":" + cnonce;
  51. }
  52. private static String HA2(String method, String uri) {
  53. return method + ":" + uri;
  54. }
  55. /**
  56. * 对输入字符串进行md5散列.
  57. */
  58. public static byte[] md5(byte[] input) {
  59. return digest(input, "MD5", null, 1);
  60. }
  61. /**
  62. * 对字符串进行散列, 支持md5与sha1算法.
  63. */
  64. private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
  65. try {
  66. MessageDigest digest = MessageDigest.getInstance(algorithm);
  67. if (salt != null) {
  68. digest.update(salt);
  69. }
  70. byte[] result = digest.digest(input);
  71. for (int i = 1; i < iterations; i++) {
  72. digest.reset();
  73. result = digest.digest(result);
  74. }
  75. return result;
  76. } catch (GeneralSecurityException e) {
  77. throw new RuntimeException(e);
  78. }
  79. }
  80. /**
  81. * 随机生成numBytes长度数组
  82. * @param numBytes
  83. * @return
  84. */
  85. public static byte[] generateSalt(int numBytes) {
  86. Validate.isTrue(numBytes > 0, "numBytes argument must be a positive integer (1 or larger)", (long) numBytes);
  87. byte[] bytes = new byte[numBytes];
  88. random.nextBytes(bytes);
  89. return bytes;
  90. }
  91. @Deprecated
  92. public static String generateSalt2(int length) {
  93. String val = "";
  94. Random random = new Random();
  95. //参数length,表示生成几位随机数
  96. for (int i = 0; i < length; i++) {
  97. String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
  98. //输出字母还是数字
  99. if ("char".equalsIgnoreCase(charOrNum)) {
  100. //输出是大写字母还是小写字母
  101. int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
  102. val += (char) (random.nextInt(26) + temp);
  103. } else if ("num".equalsIgnoreCase(charOrNum)) {
  104. val += String.valueOf(random.nextInt(10));
  105. }
  106. }
  107. return val.toLowerCase();
  108. }
  109. }