CryptoJS用法

下載地址http://code.google.com/p/crypto-js/


js代碼html

<script src="hmac-sha1.js"></script>
<script>
    var hash = CryptoJS.HmacSHA1("Message","d1419c25a711ed6725429a85a9ed951b");
    document.write(hash);
</script>

返回結果是先通過SHA1加密,而後再16進制編碼java


對應Java代碼ide

public static String encode(String data, String key) {
        byte[] byteHMAC = null;
        try {
            Mac mac = Mac.getInstance("HmacSHA1");
            SecretKeySpec spec = new SecretKeySpec(key.getBytes(), "HmacSHA1");
            mac.init(spec);
            byteHMAC = mac.doFinal(data.getBytes());
        } catch (InvalidKeyException | NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        return Hex.encodeHexString(byteHMAC);
    }
System.err.println(encode("Message","d1419c25a711ed6725429a85a9ed951b"));
相關文章
相關標籤/搜索