java equivalent to php's hmac-SHA1

前端 未结 7 869
花落未央
花落未央 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:35

    You can try this in Java:

    private static String computeSignature(String baseString, String keyString) throws GeneralSecurityException, UnsupportedEncodingException {
    
        SecretKey secretKey = null;
    
        byte[] keyBytes = keyString.getBytes();
        secretKey = new SecretKeySpec(keyBytes, "HmacSHA1");
    
        Mac mac = Mac.getInstance("HmacSHA1");
    
        mac.init(secretKey);
    
        byte[] text = baseString.getBytes();
    
        return new String(Base64.encodeBase64(mac.doFinal(text))).trim();
    }
    
    0 讨论(0)
提交回复
热议问题