Digests.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // Source code recreated from a .class file by IntelliJ IDEA
  3. // (powered by Fernflower decompiler)
  4. //
  5. package com.huimv.admin.common.utils;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.UnsupportedEncodingException;
  9. import java.security.GeneralSecurityException;
  10. import java.security.MessageDigest;
  11. import java.security.SecureRandom;
  12. import org.apache.commons.lang3.Validate;
  13. public class Digests {
  14. private static final String SHA1 = "SHA-1";
  15. private static final String MD5 = "MD5";
  16. private static SecureRandom random = new SecureRandom();
  17. public Digests() {
  18. }
  19. public static byte[] md5(byte[] input) {
  20. return digest(input, "MD5", (byte[])null, 1);
  21. }
  22. public static byte[] md5(byte[] input, int iterations) {
  23. return digest(input, "MD5", (byte[])null, iterations);
  24. }
  25. public static byte[] sha1(byte[] input) {
  26. return digest(input, "SHA-1", (byte[])null, 1);
  27. }
  28. public static byte[] sha1(byte[] input, byte[] salt) {
  29. return digest(input, "SHA-1", salt, 1);
  30. }
  31. public static byte[] sha1(byte[] input, byte[] salt, int iterations) {
  32. return digest(input, "SHA-1", salt, iterations);
  33. }
  34. private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
  35. try {
  36. MessageDigest digest = MessageDigest.getInstance(algorithm);
  37. if (salt != null) {
  38. digest.update(salt);
  39. }
  40. byte[] result = digest.digest(input);
  41. for(int i = 1; i < iterations; ++i) {
  42. digest.reset();
  43. result = digest.digest(result);
  44. }
  45. return result;
  46. } catch (GeneralSecurityException var7) {
  47. throw new RuntimeException(var7);
  48. }
  49. }
  50. public static byte[] generateSalt(int numBytes) {
  51. Validate.isTrue(numBytes > 0, "numBytes argument must be a positive integer (1 or larger)", (long)numBytes);
  52. byte[] bytes = new byte[numBytes];
  53. random.nextBytes(bytes);
  54. return bytes;
  55. }
  56. public static byte[] md5(InputStream input) throws IOException {
  57. return digest(input, "MD5");
  58. }
  59. public static byte[] sha1(InputStream input) throws IOException {
  60. return digest(input, "SHA-1");
  61. }
  62. private static byte[] digest(InputStream input, String algorithm) throws IOException {
  63. try {
  64. MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
  65. int bufferLength = 8192;
  66. byte[] buffer = new byte[bufferLength];
  67. for(int read = input.read(buffer, 0, bufferLength); read > -1; read = input.read(buffer, 0, bufferLength)) {
  68. messageDigest.update(buffer, 0, read);
  69. }
  70. return messageDigest.digest();
  71. } catch (GeneralSecurityException var6) {
  72. throw new RuntimeException(var6);
  73. }
  74. }
  75. public static final String md5(String s) {
  76. char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
  77. try {
  78. MessageDigest mdTemp = MessageDigest.getInstance("MD5");
  79. try {
  80. mdTemp.update(s.getBytes("UTF-8"));
  81. } catch (UnsupportedEncodingException var9) {
  82. mdTemp.update(s.getBytes());
  83. }
  84. byte[] md = mdTemp.digest();
  85. int j = md.length;
  86. char[] str = new char[j * 2];
  87. int k = 0;
  88. for(int i = 0; i < j; ++i) {
  89. byte byte0 = md[i];
  90. str[k++] = hexDigits[byte0 >>> 4 & 15];
  91. str[k++] = hexDigits[byte0 & 15];
  92. }
  93. return (new String(str)).toUpperCase();
  94. } catch (Exception var10) {
  95. return null;
  96. }
  97. }
  98. public static final String buildToken(String url, String paramJson, String secret) {
  99. String tempUrl = null;
  100. if (url.contains("https://")) {
  101. tempUrl = url.substring("https://".length());
  102. } else {
  103. tempUrl = url.substring("http://".length());
  104. }
  105. int index = tempUrl.indexOf("/");
  106. String URI = tempUrl.substring(index);
  107. String[] ss = URI.split("\\?");
  108. return ss.length > 1 ? md5(ss[0] + ss[1] + secret) : md5(ss[0] + paramJson + secret);
  109. }
  110. public static void main(String[] args) {
  111. System.out.println(md5("abc"));
  112. }
  113. }