我在下面的 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 方法。
有什么想法吗?
谢谢。
最佳答案
这对你不起作用吗?
$str = 'apple';
$hash = md5($str);
这将在 php 中生成一个 md5 散列。两个函数的输出不相等吗?
关于java - 相当于 Java MessageDigest MD5 的 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9583468/