private static final String HMAC_SHA1 = "HmacSHA1";
private static String toHexString(byte[] bytes) {
Formatter formatter = new Formatter();
for (byte b : bytes) {
formatter.format("%02x", b);
}
return formatter.toString();
}
private static String encrypt(String data, String key) throws NoSuchAlgorithmException, InvalidKeyException {
SecretKeySpec secret = new SecretKeySpec(key.getBytes(), HMAC_SHA1);
Mac mac = Mac.getInstance(HMAC_SHA1);
mac.init(secret);
return toHexString(mac.doFinal(data.getBytes()));
}
public static void main(String[] args) throws Exception {
String hmac = encrypt("123", "123");
System.out.println(hmac);
}