java中的MD5校验和问题
我正在接收带有校验和的 Big Endian 格式的字节数组数据。我使用以下代码创建了校验和。
public static byte[] createChecksum(byte buffer[], int len){
MessageDigest complete = MessageDigest.getInstance("MD5");
complete.update(buffer,0,len);
return complete.digest();
}
在检查数据的校验和时,它不匹配。我查过资料。数据没有损坏或错误。我还发现两个校验和之间只有中间一个字节不匹配。
I am receiving byte array data from in Big Endian format with checksum. I have created the checkcksum using following code.
public static byte[] createChecksum(byte buffer[], int len){
MessageDigest complete = MessageDigest.getInstance("MD5");
complete.update(buffer,0,len);
return complete.digest();
}
While checking the checksum with data it is not matching. I have checked the data. data is not corrupted or wrong. I also found that only one byte in middle is not matching between two checksum.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的方法看起来是正确的。
也许您得到了不想要的结果,因为您得到了一个字节数组,并且稍后需要转换为十六进制?你能写一个你正在散列的文件和结果(你得到的和你期望的)的例子吗?
您是否检查过输入字节数组的编码是否正确并且是否正确?请注意尾随行、空格等。
Your method looks correct.
Maybe you get an undesired result because you get a byte array and you need to convert to HEX later? Can you write an example of what file you are hashing and the result (you get and you expect)?
Have you checked that the input byte array has the correct encoding and is exactly what it should be? Be aware of trailing lines, spaces and so on.
可能的原因:
,但是读取以下内容
让我相信检查生成的 md5(或发送/接收它的函数)的函数中存在错误。
好吧,对于 md5 类似的哈希函数来说,这是极其不可能的(只有 1 个字节的差异),如果它再次发生,那么肯定是 snd/rcv 和/或比较结果代码时出现错误。
旁注:仅当表示占用多个字节的“基元”时,大/小端序才有意义。
possible reasons:
however reading the following
makes me believe there is an error in the function checking the resulting md5 (or the one that sends/receives it).
Well, for md5 alike hashfunctions that is exceptionally impossible (only 1 byte difference) and if it reoccurring it's definitely a mistake into snd/rcv and/or comparing the result code.
Side note: Big/Little Endian makes sense only if when representing 'primitives' that take more than a single byte.