MD5哈希值不同
我不知道如何实现从 php 到 java 的这几行..
$varInHex = "\x22\x33\xAd\xB5\x2b\xE6\x22\x33\x12\x36\x22\x31\xCA\x22\x11\x41\x62\x21\x22\x01\x55\x22\x71\x42\x10\x36";<br/><br/>
$result = md5($varInHex);
echo $result;
好吧,我尝试将它转换,但我得到了不同的结果!
byte[] seq20 = new byte[]{(byte)0x22,(byte)...etc...};
String str = seq20.toString();
String result = md5(str);
System.out.println(result);
public static String md5(String source) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = md.digest(source.getBytes("UTF-8"));
return getString(bytes);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private static String getString(byte[] bytes) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
byte b = bytes[i];
String hex = Integer.toHexString((int) 0x00FF & b);
if (hex.length() == 1) {
sb.append("0");
}
sb.append(hex);
}
return sb.toString();
}
java中的结果与php中的结果不同..
你能帮我吗? 先感谢您 :)
I don't know how to realize these few lines from php to java..
$varInHex = "\x22\x33\xAd\xB5\x2b\xE6\x22\x33\x12\x36\x22\x31\xCA\x22\x11\x41\x62\x21\x22\x01\x55\x22\x71\x42\x10\x36";<br/><br/>
$result = md5($varInHex);
echo $result;
Well, I tried to convert it but I'm getting a different result!
byte[] seq20 = new byte[]{(byte)0x22,(byte)...etc...};
String str = seq20.toString();
String result = md5(str);
System.out.println(result);
public static String md5(String source) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = md.digest(source.getBytes("UTF-8"));
return getString(bytes);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private static String getString(byte[] bytes) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
byte b = bytes[i];
String hex = Integer.toHexString((int) 0x00FF & b);
if (hex.length() == 1) {
sb.append("0");
}
sb.append(hex);
}
return sb.toString();
}
result in java is different from result in php..
Can you help me please??
Thank you in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不转换成so字符串就不能直接使用seq20吗?
我会这样做:
Can't you use seq20 directly without converting it so string?
I would do it this way:
其他两个答案都不是肯定错误的,但从优雅的角度来看,请考虑以下
编辑:
String...
varargs 的使用是完全可选的,但它使得该函数更容易一些,因为它避免了调用函数中字符串连接的开销。Neither of the other two answers are affirmatively wrong, but from an elegance standpoint, consider the following
edit: The use of
String...
varargs is totally optional, but it makes the function a little easier, as it avoids the overhead of string concatenation in the calling function.接受的解决方案:
包含一个将失败 1/256 次的实现,因为 md5sum 的输出可以有多个前导零。引发此错误的 md5 输入示例为:“15446:68106”(不带引号)。
我建议使用 apache commons DigestUtils.md5Hex 如果您需要与 php 实现匹配的 md5。
The accepted solution:
contains an implemention that will fail 1/256 times because the output of md5sum can have more than one leading zero. An example md5 input that will elicit this error is: "15446:68106" (without quotes).
I recommend using the apache commons DigestUtils.md5Hex if you need a md5 that matches the php implementation.
我的猜测是 PHP 正在将上面的内容评估为字符串,而不是十六进制。 Java 正在按照您的预期进行操作。
My guess is PHP is evaluating the above as a string, not hex. And Java is doing it as you expect it.