java equivalent to php's hmac-SHA1

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

    In fact they do agree.
    As Hans Doggen already noted PHP outputs the message digest using hexadecimal notation unless you set the raw output parameter to true.
    If you want to use the same notation in Java you can use something like

    for (byte b : digest) {
        System.out.format("%02x", b);
    }
    System.out.println();
    

    to format the output accordingly.

提交回复
热议问题