验证中...
私信发送成功
语言: Java
分类: 加密和安全
最后更新于 2017-09-13 23:17
gistfile1.txt
原始数据 复制代码
public class Md5Utils {
public static byte[] md5(String s) {
MessageDigest algorithm;
try {
algorithm = MessageDigest.getInstance("MD5");
algorithm.reset();
algorithm.update(s.getBytes("UTF-8"));
byte[] messageDigest = algorithm.digest();
return messageDigest;
} catch (Exception e) {
}
return null;
}
public static byte[] hmac(String data, String secret) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
Mac mac = Mac.getInstance("HmacSHA256");
// get the bytes of the hmac key and data string
byte[] secretByte = secret.getBytes("UTF-8");
byte[] dataBytes = data.getBytes("UTF-8");
SecretKey secretKey = new SecretKeySpec(secretByte, "HMACSHA256");
mac.init(secretKey);
return mac.doFinal(dataBytes);
}
public static final String toHex(byte hash[]) {
if (hash == null) {
return null;
}
StringBuffer buf = new StringBuffer(hash.length * 2);
int i;
for (i = 0; i < hash.length; i++) {
if ((hash[i] & 0xff) < 0x10) {
buf.append("0");
}
buf.append(Long.toString(hash[i] & 0xff, 16));
}
return buf.toString();
}
public static String hash(String data, String key) {
try {
return new String(toHex(hmac(data, key)).getBytes("UTF-8"), "UTF-8");
} catch (Exception e) {
return data;
}
}
/**
* 返回32位大写MD5�?
* @param content
* @return 异常返回null 转换成功返回 32位大写MD5
*/
private static String getEncode32MD5(String content){
try {
MessageDigest digest = MessageDigest.getInstance("MD5");
digest.update(content.getBytes("UTF-8"));
StringBuilder builder = new StringBuilder();
for (byte b : digest.digest()) {
builder.append(Integer.toHexString((b >> 4) & 0xf));
builder.append(Integer.toHexString(b & 0xf));
}
return builder.toString().toUpperCase();
} catch (Exception e) {
return null;
}
}
private final static String salt = "xingyuanapp2016";
public static String get32MD5(String content){
return getEncode32MD5(content+salt);
}
public static String getLessonZipNameMD5(String content){
return getEncode32MD5(content);
}
}

评论列表( 0 )

你可以在登录后,对此项目发表评论