GCommonUtils.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package com.bbeng.www.GameCenter;
  2. import org.apache.commons.codec.binary.Hex;
  3. import org.apache.commons.logging.Log;
  4. import org.apache.commons.logging.LogFactory;
  5. import org.springframework.util.StringUtils;
  6. import javax.crypto.Mac;
  7. import javax.crypto.SecretKey;
  8. import javax.crypto.spec.SecretKeySpec;
  9. import java.io.UnsupportedEncodingException;
  10. import java.security.InvalidKeyException;
  11. import java.security.MessageDigest;
  12. import java.security.NoSuchAlgorithmException;
  13. import java.util.UUID;
  14. /**
  15. * 综合工具类
  16. *
  17. * @author 卜长清,buchangqing@goluk.com
  18. * @version 1.0.0
  19. * @date 2015年9月16日 下午4:13:05
  20. */
  21. public class GCommonUtils {
  22. private static Log LOG = LogFactory.getLog(GCommonUtils.class);
  23. // ================================================================================
  24. // UUID
  25. // ================================================================================
  26. private static String[] chars = new String[]{"a", "b", "c", "d", "e", "f",
  27. "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
  28. "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
  29. "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
  30. "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
  31. "W", "X", "Y", "Z"};
  32. /**
  33. * 8 digital short UUID
  34. *
  35. * @return
  36. */
  37. public static String shortUuid() {
  38. String uuid = GCommonUtils.uuid();
  39. StringBuffer shortBuffer = new StringBuffer();
  40. for (int i = 0; i < 8; i++) {
  41. String str = uuid.substring(i * 4, i * 4 + 4);
  42. int x = Integer.parseInt(str, 16);
  43. shortBuffer.append(chars[x % 0x3E]);
  44. }
  45. return shortBuffer.toString();
  46. }
  47. /**
  48. * 32 digital normal UUID
  49. *
  50. * @return
  51. */
  52. public static String uuid() {
  53. return UUID.randomUUID().toString().replace("-", "");
  54. }
  55. /**
  56. * 获取动态长度的 UUID
  57. */
  58. public static String dynamicUuid(int size) {
  59. String uuid = "";
  60. for (int i = 0; i < size / 8; i++) {
  61. uuid += shortUuid();
  62. }
  63. int remain = size % 8;
  64. if (remain > 0) {
  65. uuid += shortUuid().substring(0, remain);
  66. }
  67. return uuid;
  68. }
  69. public static String dynamicUuid(int size, String prefix) {
  70. return String.format("%s%s", prefix, dynamicUuid(size));
  71. }
  72. // ================================================================================
  73. // HASH
  74. // ================================================================================
  75. /**
  76. * HMAC签名
  77. *
  78. * @param key 键
  79. * @param data 数据
  80. * @return 签名
  81. */
  82. public static String getHmacSHA256(String key, String data) {
  83. String sign = "";
  84. try {
  85. if (StringUtils.isEmpty(key) || StringUtils.isEmpty(data)) {
  86. sign = "Missing argument";
  87. return sign;
  88. }
  89. Mac mac = Mac.getInstance("HMACSHA256");
  90. byte[] secretByte = key.getBytes("UTF-8");
  91. byte[] dataBytes = data.getBytes("UTF-8");
  92. SecretKey secret = new SecretKeySpec(secretByte, "HMACSHA256");
  93. mac.init(secret);
  94. byte[] doFinal = mac.doFinal(dataBytes);
  95. byte[] hex = new Hex().encode(doFinal);
  96. sign = new String(hex);
  97. } catch (NoSuchAlgorithmException e) {
  98. e.printStackTrace();
  99. } catch (UnsupportedEncodingException e) {
  100. e.printStackTrace();
  101. } catch (InvalidKeyException e) {
  102. e.printStackTrace();
  103. } catch (Exception e) {
  104. e.printStackTrace();
  105. }
  106. return sign;
  107. }
  108. /**
  109. * 获取数据数字Hash
  110. *
  111. * @param data
  112. * @param algorithm following algorithms:
  113. * - MD5
  114. * - SHA-1
  115. * - SHA-224
  116. * - SHA-256
  117. * - SHA-384
  118. * - SHA-512
  119. * @return
  120. */
  121. public static String getHash(String data, String algorithm) {
  122. String hash = "";
  123. if (StringUtils.isEmpty(data)) {
  124. return hash;
  125. }
  126. try {
  127. MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
  128. messageDigest.update(data.getBytes());
  129. byte bytes[] = messageDigest.digest();
  130. StringBuffer hexString = new StringBuffer();
  131. for (int i = 0; i < bytes.length; i++) {
  132. String hex = Integer.toHexString(0xff & bytes[i]);
  133. if (hex.length() == 1) {
  134. hexString.append('0');
  135. }
  136. hexString.append(hex);
  137. }
  138. hash = hexString.toString();
  139. } catch (NoSuchAlgorithmException e) {
  140. e.printStackTrace();
  141. }
  142. return hash.toLowerCase();
  143. }
  144. }