/**
|
* Copyright (c) 2018 人人开源 All rights reserved.
|
*
|
* https://www.renren.io
|
*
|
* 版权所有,侵权必究!
|
*/
|
|
package com.zt.core.shiro;
|
|
import com.zt.common.exception.RenException;
|
|
import java.security.MessageDigest;
|
import java.util.UUID;
|
|
/**
|
* 生成token
|
*
|
* @author Mark sunlightcs@gmail.com
|
*/
|
public class TokenGenerator {
|
|
public static String generateValue() {
|
return generateValue(UUID.randomUUID().toString());
|
}
|
|
private static final char[] HEX_CODE = "0123456789abcdef".toCharArray();
|
|
public static String toHexString(byte[] data) {
|
if(data == null) {
|
return null;
|
}
|
StringBuilder r = new StringBuilder(data.length*2);
|
for ( byte b : data) {
|
r.append(HEX_CODE[(b >> 4) & 0xF]);
|
r.append(HEX_CODE[(b & 0xF)]);
|
}
|
return r.toString();
|
}
|
|
public static String generateValue(String param) {
|
try {
|
MessageDigest algorithm = MessageDigest.getInstance("MD5");
|
algorithm.reset();
|
algorithm.update(param.getBytes());
|
byte[] messageDigest = algorithm.digest();
|
return toHexString(messageDigest);
|
} catch (Exception e) {
|
throw new RenException("token invalid", e);
|
}
|
}
|
}
|