| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- package com.bbeng.www.GameCenter;
- import org.apache.commons.codec.binary.Hex;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.springframework.util.StringUtils;
- import javax.crypto.Mac;
- import javax.crypto.SecretKey;
- import javax.crypto.spec.SecretKeySpec;
- import java.io.UnsupportedEncodingException;
- import java.security.InvalidKeyException;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import java.util.UUID;
- /**
- * 综合工具类
- *
- * @author 卜长清,buchangqing@goluk.com
- * @version 1.0.0
- * @date 2015年9月16日 下午4:13:05
- */
- public class GCommonUtils {
- private static Log LOG = LogFactory.getLog(GCommonUtils.class);
- // ================================================================================
- // UUID
- // ================================================================================
- private static String[] chars = new String[]{"a", "b", "c", "d", "e", "f",
- "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
- "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
- "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
- "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
- "W", "X", "Y", "Z"};
- /**
- * 8 digital short UUID
- *
- * @return
- */
- public static String shortUuid() {
- String uuid = GCommonUtils.uuid();
- StringBuffer shortBuffer = new StringBuffer();
- for (int i = 0; i < 8; i++) {
- String str = uuid.substring(i * 4, i * 4 + 4);
- int x = Integer.parseInt(str, 16);
- shortBuffer.append(chars[x % 0x3E]);
- }
- return shortBuffer.toString();
- }
- /**
- * 32 digital normal UUID
- *
- * @return
- */
- public static String uuid() {
- return UUID.randomUUID().toString().replace("-", "");
- }
- /**
- * 获取动态长度的 UUID
- */
- public static String dynamicUuid(int size) {
- String uuid = "";
- for (int i = 0; i < size / 8; i++) {
- uuid += shortUuid();
- }
- int remain = size % 8;
- if (remain > 0) {
- uuid += shortUuid().substring(0, remain);
- }
- return uuid;
- }
- public static String dynamicUuid(int size, String prefix) {
- return String.format("%s%s", prefix, dynamicUuid(size));
- }
- // ================================================================================
- // HASH
- // ================================================================================
- /**
- * HMAC签名
- *
- * @param key 键
- * @param data 数据
- * @return 签名
- */
- public static String getHmacSHA256(String key, String data) {
- String sign = "";
- try {
- if (StringUtils.isEmpty(key) || StringUtils.isEmpty(data)) {
- sign = "Missing argument";
- return sign;
- }
- Mac mac = Mac.getInstance("HMACSHA256");
- byte[] secretByte = key.getBytes("UTF-8");
- byte[] dataBytes = data.getBytes("UTF-8");
- SecretKey secret = new SecretKeySpec(secretByte, "HMACSHA256");
- mac.init(secret);
- byte[] doFinal = mac.doFinal(dataBytes);
- byte[] hex = new Hex().encode(doFinal);
- sign = new String(hex);
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- } catch (InvalidKeyException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return sign;
- }
- /**
- * 获取数据数字Hash
- *
- * @param data
- * @param algorithm following algorithms:
- * - MD5
- * - SHA-1
- * - SHA-224
- * - SHA-256
- * - SHA-384
- * - SHA-512
- * @return
- */
- public static String getHash(String data, String algorithm) {
- String hash = "";
- if (StringUtils.isEmpty(data)) {
- return hash;
- }
- try {
- MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
- messageDigest.update(data.getBytes());
- byte bytes[] = messageDigest.digest();
- StringBuffer hexString = new StringBuffer();
- for (int i = 0; i < bytes.length; i++) {
- String hex = Integer.toHexString(0xff & bytes[i]);
- if (hex.length() == 1) {
- hexString.append('0');
- }
- hexString.append(hex);
- }
- hash = hexString.toString();
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- }
- return hash.toLowerCase();
- }
- }
|