java equivalent to php's hmac-SHA1

Posted by Bee on Stack Overflow See other posts from Stack Overflow or by Bee
Published on 2009-10-22T20:56:57Z Indexed on 2010/05/21 21:40 UTC
Read the original article Hit count: 396

Filed under:
|
|

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 two do not agree:

String mykey = "secret";
String test = "test";
try {
    Mac mac = Mac.getInstance("HmacSHA1");
    SecretKeySpec secret = new SecretKeySpec(mykey.getBytes(),"HmacSHA1");
    mac.init(secret);
    byte[] digest = mac.doFinal(test.getBytes());
    String enc = new String(digest);
    System.out.println(enc);  
} catch (Exception e) {
    System.out.println(e.getMessage());
}

The outputs with key = "secret" and test = "test" do not seem to match.

© Stack Overflow or respective owner

Related posts about java

Related posts about php