51

我正在寻找与此 php 调用等效的 java:

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

我试过这个,使用java.crypto.Mac,但两者不同意:

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());
}

key = "secret" 和 test = "test" 的输​​出似乎不匹配。

4

7 回答 7

41

事实上,他们确实同意。
正如 Hans Doggen 已经指出的那样,除非您将原始输出参数设置为 true,否则 PHP 使用十六进制表示法输出消息摘要。
如果你想在 Java 中使用相同的符号,你可以使用类似的东西

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

相应地格式化输出。

于 2009-10-22T22:28:15.637 回答
20

你可以在 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();
}
于 2011-05-25T12:04:26.700 回答
5

这是我的实现:

        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;
于 2011-05-11T18:40:21.527 回答
3

在我看来,PHP 对 Java 产生的字节 (1a = 26) 使用 HEX 表示法 - 但我没有检查整个表达式。

如果通过本页的方法运行字节数组会发生什么

于 2009-10-22T21:18:00.070 回答
3

我对 HmacMD5 的实现 - 只需将算法更改为 HmacSHA1:

SecretKeySpec keySpec = new SecretKeySpec("secretkey".getBytes(), "HmacMD5");
Mac mac = Mac.getInstance("HmacMD5");
mac.init(keySpec);
byte[] hashBytes = mac.doFinal("text2crypt".getBytes());
return Hex.encodeHexString(hashBytes);
于 2013-05-13T09:57:19.177 回答
3

这样我可以得到与在 php 中使用 hash_hmac 得到的字符串完全相同的字符串

String result;

try {
        String data = "mydata";
        String key = "myKey";
        // Get an hmac_sha1 key from the raw key bytes
        byte[] keyBytes = key.getBytes();
        SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1");

        // Get an hmac_sha1 Mac instance and initialize with the signing key
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(signingKey);

        // Compute the hmac on input data bytes
        byte[] rawHmac = mac.doFinal(data.getBytes());

        // Convert raw bytes to Hex
        byte[] hexBytes = new Hex().encode(rawHmac);

        //  Covert array of Hex bytes to a String
        result = new String(hexBytes, "ISO-8859-1");
        out.println("MAC : " + result);
}
catch (Exception e) {

}
于 2014-10-10T10:06:26.380 回答
1

没有测试过,但试试这个:

        BigInteger hash = new BigInteger(1, digest);
        String enc = hash.toString(16);
        if ((enc.length() % 2) != 0) {
            enc = "0" + enc;
        }

这是我的方法的快照,它使 java 的 md5 和 sha1 匹配 php。

于 2009-10-22T21:22:39.007 回答