Digests.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * Copyright (c) 2005-2012 springside.org.cn
  3. */
  4. package com.jeeplus.common.security;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.security.GeneralSecurityException;
  8. import java.security.MessageDigest;
  9. import java.security.SecureRandom;
  10. import org.apache.commons.lang3.Validate;
  11. import com.jeeplus.common.utils.Exceptions;
  12. /**
  13. * 支持SHA-1/MD5消息摘要的工具类.
  14. *
  15. * 返回ByteSource,可进一步被编码为Hex, Base64或UrlSafeBase64
  16. *
  17. * @author calvin
  18. */
  19. public class Digests {
  20. private static final String SHA1 = "SHA-1";
  21. private static final String MD5 = "MD5";
  22. private static SecureRandom random = new SecureRandom();
  23. /**
  24. * 对输入字符串进行md5散列.
  25. */
  26. public static byte[] md5(byte[] input) {
  27. return digest(input, MD5, null, 1);
  28. }
  29. public static byte[] md5(byte[] input, int iterations) {
  30. return digest(input, MD5, null, iterations);
  31. }
  32. /**
  33. * 对输入字符串进行sha1散列.
  34. */
  35. public static byte[] sha1(byte[] input) {
  36. return digest(input, SHA1, null, 1);
  37. }
  38. public static byte[] sha1(byte[] input, byte[] salt) {
  39. return digest(input, SHA1, salt, 1);
  40. }
  41. public static byte[] sha1(byte[] input, byte[] salt, int iterations) {
  42. return digest(input, SHA1, salt, iterations);
  43. }
  44. /**
  45. * 对字符串进行散列, 支持md5与sha1算法.
  46. */
  47. private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
  48. try {
  49. MessageDigest digest = MessageDigest.getInstance(algorithm);
  50. if (salt != null) {
  51. digest.update(salt);
  52. }
  53. byte[] result = digest.digest(input);
  54. for (int i = 1; i < iterations; i++) {
  55. digest.reset();
  56. result = digest.digest(result);
  57. }
  58. return result;
  59. } catch (GeneralSecurityException e) {
  60. throw Exceptions.unchecked(e);
  61. }
  62. }
  63. /**
  64. * 生成随机的Byte[]作为salt.
  65. *
  66. * @param numBytes byte数组的大小
  67. */
  68. public static byte[] generateSalt(int numBytes) {
  69. Validate.isTrue(numBytes > 0, "numBytes argument must be a positive integer (1 or larger)", numBytes);
  70. byte[] bytes = new byte[numBytes];
  71. random.nextBytes(bytes);
  72. return bytes;
  73. }
  74. /**
  75. * 对文件进行md5散列.
  76. */
  77. public static byte[] md5(InputStream input) throws IOException {
  78. return digest(input, MD5);
  79. }
  80. /**
  81. * 对文件进行sha1散列.
  82. */
  83. public static byte[] sha1(InputStream input) throws IOException {
  84. return digest(input, SHA1);
  85. }
  86. private static byte[] digest(InputStream input, String algorithm) throws IOException {
  87. try {
  88. MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
  89. int bufferLength = 8 * 1024;
  90. byte[] buffer = new byte[bufferLength];
  91. int read = input.read(buffer, 0, bufferLength);
  92. while (read > -1) {
  93. messageDigest.update(buffer, 0, read);
  94. read = input.read(buffer, 0, bufferLength);
  95. }
  96. return messageDigest.digest();
  97. } catch (GeneralSecurityException e) {
  98. throw Exceptions.unchecked(e);
  99. }
  100. }
  101. public static String string2MD5(String inStr){
  102. MessageDigest md5 = null;
  103. try{
  104. md5 = MessageDigest.getInstance("MD5");
  105. }catch (Exception e){
  106. System.out.println(e.toString());
  107. e.printStackTrace();
  108. return "";
  109. }
  110. char[] charArray = inStr.toCharArray();
  111. byte[] byteArray = new byte[charArray.length];
  112. for (int i = 0; i < charArray.length; i++)
  113. byteArray[i] = (byte) charArray[i];
  114. byte[] md5Bytes = md5.digest(byteArray);
  115. StringBuffer hexValue = new StringBuffer();
  116. for (int i = 0; i < md5Bytes.length; i++){
  117. int val = ((int) md5Bytes[i]) & 0xff;
  118. if (val < 16)
  119. hexValue.append("0");
  120. hexValue.append(Integer.toHexString(val));
  121. }
  122. return hexValue.toString();
  123. }
  124. }