java equivalent to php's hmac-SHA1

前端 未结 7 937
花落未央
花落未央 2020-11-28 20:33

I\'m looking for a java equivalent to this php call:

hash_hmac(\'sha1\', \"test\", \"secret\")

I tried this, using java.crypto.Mac, but the

7条回答
  •  悲哀的现实
    2020-11-28 21:31

    This is my implementation :

            String hmac = "";
    
        Mac mac = Mac.getInstance("HmacSHA1");
        SecretKeySpec secret = new SecretKeySpec(llave.getBytes(), "HmacSHA1");
        mac.init(secret);
        byte[] digest = mac.doFinal(cadena.getBytes());
        BigInteger hash = new BigInteger(1, digest);
        hmac = hash.toString(16);
    
        if (hmac.length() % 2 != 0) {
            hmac = "0" + hmac;
        }
    
        return hmac;
    

提交回复
热议问题