1 2 3 4 5 6 7 8 9 10 11
|
Sha1加密:
private static final char[] _HEX_DIGITS_ = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
private static String getFormattedText(byte[] bytes) { int len = bytes.length; StringBuilder buf = new StringBuilder(len * 2); // 把密文转换成十六进制的字符串形式 for (int j = 0; j < len; j++) { buf.append(_HEX_DIGITS_[(bytes[j] >> 4) & 0x0f]); buf.append(_HEX_DIGITS_[bytes[j] & 0x0f]); } return buf.toString(); }
public String payload(@RequestBody String body, HttpServletRequest request) {
MessageDigest sha1 = MessageDigest.getInstance("SHA-1"); String secret = "123321Zh"; sha1.update(secret.getBytes("utf-8")); byte[] digest = sha1.digest(); String result = _getFormattedText_(digest);
}
|
Java HmacSHA1算法:(HMAC(散列消息身份验证码: Hashed Message Authentication Code))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
public static String hmacSha1(String src, String key) {
try {
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes("utf-8"), "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signingKey);
byte[] rawHmac = mac.doFinal(src.getBytes("utf-8"));
return Hex.encodeHexString(rawHmac);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
|
近期评论