Digest.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package com.caimei.utils;
  2. import java.io.UnsupportedEncodingException;
  3. import java.security.MessageDigest;
  4. import java.security.NoSuchAlgorithmException;
  5. import java.util.Arrays;
  6. public class Digest {
  7. public static final String ENCODE = "UTF-8";
  8. public static String signMD5(String aValue, String encoding) {
  9. try {
  10. byte[] input = aValue.getBytes(encoding);
  11. MessageDigest md = MessageDigest.getInstance("MD5");
  12. return ConvertUtils.toHex(md.digest(input));
  13. } catch (NoSuchAlgorithmException e) {
  14. e.printStackTrace();
  15. return null;
  16. } catch (UnsupportedEncodingException e) {
  17. e.printStackTrace();
  18. return null;
  19. }
  20. }
  21. public static String hmacSign(String aValue) {
  22. try {
  23. byte[] input = aValue.getBytes();
  24. MessageDigest md = MessageDigest.getInstance("MD5");
  25. return ConvertUtils.toHex(md.digest(input));
  26. } catch (NoSuchAlgorithmException e) {
  27. e.printStackTrace();
  28. return null;
  29. }
  30. }
  31. public static String hmacSign(String aValue, String aKey) {
  32. return hmacSign(aValue, aKey, ENCODE);
  33. }
  34. public static String hmacSign(String aValue, String aKey, String encoding) {
  35. byte k_ipad[] = new byte[64];
  36. byte k_opad[] = new byte[64];
  37. byte keyb[];
  38. byte value[];
  39. try {
  40. keyb = aKey.getBytes(encoding);
  41. value = aValue.getBytes(encoding);
  42. } catch (UnsupportedEncodingException e) {
  43. keyb = aKey.getBytes();
  44. value = aValue.getBytes();
  45. }
  46. Arrays.fill(k_ipad, keyb.length, 64, (byte) 54);
  47. Arrays.fill(k_opad, keyb.length, 64, (byte) 92);
  48. for (int i = 0; i < keyb.length; i++) {
  49. k_ipad[i] = (byte) (keyb[i] ^ 0x36);
  50. k_opad[i] = (byte) (keyb[i] ^ 0x5c);
  51. }
  52. MessageDigest md = null;
  53. try {
  54. md = MessageDigest.getInstance("MD5");
  55. } catch (NoSuchAlgorithmException e) {
  56. e.printStackTrace();
  57. return null;
  58. }
  59. md.update(k_ipad);
  60. md.update(value);
  61. byte dg[] = md.digest();
  62. md.reset();
  63. md.update(k_opad);
  64. md.update(dg, 0, 16);
  65. dg = md.digest();
  66. return ConvertUtils.toHex(dg);
  67. }
  68. public static String hmacSHASign(String aValue, String aKey, String encoding) {
  69. byte k_ipad[] = new byte[64];
  70. byte k_opad[] = new byte[64];
  71. byte keyb[];
  72. byte value[];
  73. try {
  74. keyb = aKey.getBytes(encoding);
  75. value = aValue.getBytes(encoding);
  76. } catch (UnsupportedEncodingException e) {
  77. keyb = aKey.getBytes();
  78. value = aValue.getBytes();
  79. }
  80. Arrays.fill(k_ipad, keyb.length, 64, (byte) 54);
  81. Arrays.fill(k_opad, keyb.length, 64, (byte) 92);
  82. for (int i = 0; i < keyb.length; i++) {
  83. k_ipad[i] = (byte) (keyb[i] ^ 0x36);
  84. k_opad[i] = (byte) (keyb[i] ^ 0x5c);
  85. }
  86. MessageDigest md = null;
  87. try {
  88. md = MessageDigest.getInstance("SHA");
  89. } catch (NoSuchAlgorithmException e) {
  90. e.printStackTrace();
  91. return null;
  92. }
  93. md.update(k_ipad);
  94. md.update(value);
  95. byte dg[] = md.digest();
  96. md.reset();
  97. md.update(k_opad);
  98. md.update(dg, 0, 20);
  99. dg = md.digest();
  100. return ConvertUtils.toHex(dg);
  101. }
  102. public static String digest(String aValue) {
  103. return digest(aValue, ENCODE);
  104. }
  105. public static String digest(String aValue, String encoding) {
  106. aValue = aValue.trim();
  107. byte value[];
  108. try {
  109. value = aValue.getBytes(encoding);
  110. } catch (UnsupportedEncodingException e) {
  111. value = aValue.getBytes();
  112. }
  113. MessageDigest md = null;
  114. try {
  115. md = MessageDigest.getInstance("SHA");
  116. } catch (NoSuchAlgorithmException e) {
  117. e.printStackTrace();
  118. return null;
  119. }
  120. return ConvertUtils.toHex(md.digest(value));
  121. }
  122. public static String digest(String aValue, String alg, String encoding) {
  123. aValue = aValue.trim();
  124. byte value[];
  125. try {
  126. value = aValue.getBytes(encoding);
  127. } catch (UnsupportedEncodingException e) {
  128. value = aValue.getBytes();
  129. }
  130. MessageDigest md = null;
  131. try {
  132. md = MessageDigest.getInstance(alg);
  133. } catch (NoSuchAlgorithmException e) {
  134. e.printStackTrace();
  135. return null;
  136. }
  137. return ConvertUtils.toHex(md.digest(value));
  138. }
  139. public static String udpSign(String aValue) {
  140. try {
  141. byte[] input = aValue.getBytes("UTF-8");
  142. MessageDigest md = MessageDigest.getInstance("SHA1");
  143. return new String(Base64.encode(md.digest(input)), ENCODE);
  144. } catch (Exception e) {
  145. return null;
  146. }
  147. }
  148. }