import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
public class HmacUtil {
public static String computeHmacSha512(String plainText, String secretKey) throws NoSuchAlgorithmException, InvalidKeyException {
Mac hmacSha512 = Mac.getInstance("HmacSHA512");
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), "HmacSHA512");
hmacSha512.init(secretKeySpec);
byte[] hash = hmacSha512.doFinal(plainText.getBytes());
return Base64.getEncoder().encodeToString(hash);
}
}
|
|