JAVA HMAC SHA256位加密算法

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class HMAC {
    public static void main(String[] args) throws Exception {
        String s=HMACSHA256("123","21+20t4jm4DlkMv3nA5OSf76GrH+ifEORkO3T2yztec=");
        System.out.println(s);
    }




    public static String HMACSHA256(String data, String key) throws Exception {
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256"));
        byte[] signData = mac.doFinal(data.getBytes("UTF-8"));
        StringBuilder sb = new StringBuilder();
        for (byte item : signData) {
            sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString().toUpperCase();
    }

}

算出来的结果跟Delphi ,还有网上的一样

猜你喜欢

转载自blog.csdn.net/wh445306/article/details/115015237