我在java下面有这个方法,并且需要PHP中的完全等价物,因为两个哈希值都要进行比较..
Java方法是:
public String getMD5(String inStr)
{
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (Exception e) {
e.printStackTrace();
}
char[] charArray = inStr.toCharArray();
byte[] byteArray = new byte[charArray.length];
for (int i = 0; i < charArray.length; i++)
byteArray[i] = (byte) charArray[i];
byte[] md5Bytes = md5.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16)
hexValue.append("0");
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
我目前正在使用php的crypt方法。
有什么想法吗?
感谢。
答案 0 :(得分:5)
这不适合你吗?
$str = 'apple';
$hash = md5($str);
这将在php中生成md5哈希。两个函数的输出不相等吗?