大家好,我是开发java的,最近遇到一个和php相关的问题,想和大家请教一下
hash_hmac(“MD5”, "a=1&b=data&c=2","私钥")
php的这个函数内部是怎么实现的,我目前需要用java做一个签名函数,和php的hash_hmac函数功能一致,网上搜索的都使用不了,所以想知道这个函数底层源码是怎么实现的,这样我就可以用java实现了
搜索到一个阿里的技术文档,内容如下,但是无法使用:
public static byte[] encryptHMAC(String data, String secret) throws Exception {
byte[] bytes = null;
try {
SecretKey secretKey = new SecretKeySpec(secret.getBytes(Constants.ENCODE_UTF8), "HmacMD5");
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
bytes = mac.doFinal(data.getBytes(Constants.ENCODE_UTF8));
} catch (Exception gse) {
throw new IOException(gse.toString());
}
return bytes;
}
public static String byte2hex(byte[] bytes) {
StringBuilder sign = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(bytes[i] & 0xFF);
if (hex.length() == 1) {
sign.append("0");
}
sign.append(hex.toUpperCase());
}
System.out.println(sign.toString().toLowerCase());
return sign.toString().toLowerCase();
}
请各位php的开发者,说一下这个函数的 hash_hmac(“MD5”, "a=1&b=data&c=2","私钥") 底层实现原理