81,092
社区成员
发帖
与我相关
我的任务
分享
private final static String KEY_MAC = "HmacMD5";
private final static String UTF_8_CHARSET = "UTF-8";
public static String getHmacMD5String(String plainText, String salt) throws NoSuchAlgorithmException, InvalidKeyException, IOException {
String sEncodedString = null;
SecretKey key = new SecretKeySpec(salt.getBytes(UTF_8_CHARSET), KEY_MAC);
Mac mac = Mac.getInstance(KEY_MAC);
mac.init(key);
byte[] bytes = mac.doFinal(plainText.getBytes(UTF_8_CHARSET));
StringBuffer hash = new StringBuffer();
for (int i = 0, len = bytes.length; i < len; i++) {
String hex = Integer.toHexString(0xFF & bytes[i]);
if (hex.length() == 1) {
hash.append('0');
}
hash.append(hex);
}
sEncodedString = hash.toString();
return sEncodedString;
}
[/quote]
感谢大神分享!
private final static String KEY_MAC = "HmacMD5";
private final static String UTF_8_CHARSET = "UTF-8";
public static String getHmacMD5String(String plainText, String salt) throws NoSuchAlgorithmException, InvalidKeyException, IOException {
String sEncodedString = null;
SecretKey key = new SecretKeySpec(salt.getBytes(UTF_8_CHARSET), KEY_MAC);
Mac mac = Mac.getInstance(KEY_MAC);
mac.init(key);
byte[] bytes = mac.doFinal(plainText.getBytes(UTF_8_CHARSET));
StringBuffer hash = new StringBuffer();
for (int i = 0, len = bytes.length; i < len; i++) {
String hex = Integer.toHexString(0xFF & bytes[i]);
if (hex.length() == 1) {
hash.append('0');
}
hash.append(hex);
}
sEncodedString = hash.toString();
return sEncodedString;
}