com.hn.pay.wxpay.utils.HashKit Maven / Gradle / Ivy
package com.hn.pay.wxpay.utils;
import java.security.MessageDigest;
public class HashKit {
private static final java.security.SecureRandom random = new java.security.SecureRandom();
private static final char[] HEX_DIGITS = "0123456789abcdef".toCharArray();
private static final char[] CHAR_ARRAY = "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
/**
* md5加密
* @param srcStr srcStr
* @return 加密后字符串
*/
public static String md5(String srcStr){
return hash("MD5", srcStr);
}
/**
*
* @param algorithm algorithm
* @param srcStr srcStr
* @return {String}
*/
public static String hash(String algorithm, String srcStr) {
try {
MessageDigest md = MessageDigest.getInstance(algorithm);
byte[] bytes = md.digest(srcStr.getBytes("utf-8"));
return toHex(bytes);
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* toHex
* @param bytes bytes
* @return 十六进制
*/
private static String toHex(byte[] bytes) {
StringBuilder ret = new StringBuilder(bytes.length * 2);
for (int i=0; i> 4) & 0x0f]);
ret.append(HEX_DIGITS[bytes[i] & 0x0f]);
}
return ret.toString();
}
/**
* slowEquals
* @param a a
* @param b b
* @return true or false
*/
public static boolean slowEquals(byte[] a, byte[] b) {
if (a == null || b == null) {
return false;
}
int diff = a.length ^ b.length;
for(int i=0; i
© 2015 - 2024 Weber Informatics LLC | Privacy Policy