希望这可以帮助:
import javax.crypto.Mac;import javax.crypto.spec.SecretKeySpec;import java.io.UnsupportedEncodingException;import java.nio.charset.StandardCharsets;import java.security.InvalidKeyException;import java.security.NoSuchAlgorithmException;import java.util.base64;public class Test1 { private static final String HMAC_SHA512 = "HmacSHA512"; public static void main(String[] args) { Mac sha512Hmac; String result; final String key = "Welcome1"; try { final byte[] byteKey = key.getBytes(StandardCharsets.UTF_8); sha512Hmac = Mac.getInstance(HMAC_SHA512); SecretKeySpec keySpec = new SecretKeySpec(byteKey, HMAC_SHA512); sha512Hmac.init(keySpec); byte[] macData = sha512Hmac.doFinal("My message".getBytes(StandardCharsets.UTF_8)); // Can either base64 enpre or put it right into hex result = base64.getEnprer().enpreToString(macData); //result = bytesToHex(macData); } catch (UnsupportedEncodingException | InvalidKeyException | NoSuchAlgorithmException e) { e.printStackTrace(); } finally { // Put any cleanup here System.out.println("Done"); } }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)