I have a method which is generating HMAC value from public and private key.
Here is my method code:
String mykey = "fb6a1271f98099ac96cc0002d5e8022b";
String test = "json6b17f33e25e2d8197462d1c6bcb0b1302156641988";
try {
Mac mac = Mac.getInstance("HmacSHA1");
SecretKeySpec secret = new SecretKeySpec(mykey.getBytes(),
"HmacSHA1");
mac.init(secret);
byte[] digest = mac.doFinal(test.getBytes());
for (byte b : digest) {
System.out.format("%02x", b);
}
System.out.println();
} catch (Exception e) {
System.out.println(e.getMessage());
}
Now as per requirement, I need to use the value returned from it as String.
Here is the value returned from method in
System.out.format("%02x", b); =bd0aea241e88c8a22692eba02887ad97a220f827
Please help me..
I would recommend something like this (use a StringBuilder)
StringBuilder sb = new StringBuilder(digest.length * 2);
Formatter formatter = new Formatter(sb);
for (byte b : digest) {
formatter.format("%02x", b);
}
return sb.toString();
如果您要查找字符串的十六进制形式,则BigInteger
具有出色的基本转换功能:
return new BigInteger(mac.doFinal(test.getBytes())).toString(16);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.