简体   繁体   English

Javascript md5图像哈希与Java进行比较

[英]Javascript md5 hash of image to compare with Java

I am getting a different hash from javascript then I do from java ... I am hoping someone can explain and help to correct this. 我从javascript得到一个不同的哈希然后我从java ...我希望有人可以解释并帮助纠正这一点。

The History I am trying to connect to a third party from a mobile app.. I am having to rewrite their java api into javascript which is causing some translation problems. 历史我试图从移动应用程序连接到第三方..我不得不将他们的java api重写为javascript,这导致一些翻译问题。 One of the issues is that they are expecting a data array of an image and an md5 hash of that image for comparison. 其中一个问题是他们期望图像的数据数组和该图像的md5哈希值进行比较。 In Java when I use their code I get a different hash to when I do the same thing in javascript. 在Java中,当我使用他们的代码时,我得到一个不同的哈希,当我在javascript中做同样的事情。

In java, the code: 在java中,代码:

byte[] decodedString = Base64.decode(testimg,  Base64.DEFAULT);

MessageDigest md = MessageDigest.getInstance("MD5");
md.reset();
md.update(decodedString);

byte[] array = md.digest();

creates a hash of: ad5e3cdf9174167243ded0e329a37cf6 创建一个哈希:ad5e3cdf9174167243ded0e329a37cf6

the same thing in javascript gives: 8bd54f4d1c4800e3df9cdd2aee728ded javascript中的相同内容给出:8bd54f4d1c4800e3df9cdd2aee728ded

var decodedstr = window.atob(testimg);
var hashString = MD5(decodedstr);

In javascript I cannot pass a byte array as with java, but the byte values should be the same (although java displays these as unsigned) 在javascript中,我不能像java一样传递字节数组,但字节值应该相同(尽管java将这些显示为unsigned)

How can I change my image array in javascript to create the same hash and still have a proper image? 如何在javascript中更改我的图像数组以创建相同的哈希并且仍然具有正确的图像?

Finally got it! 终于明白了! Thanks guys for your help and comments. 谢谢你们的帮助和评论。 For anyone else who stumbles upon this: Eventually found an MD5 script at: http://blog.faultylabs.com/?d=md5 that will allow me to pass in an arraybuffer and that resolved it. 对于偶然发现这一点的其他人:最终在http://blog.faultylabs.com/?d=md5找到了一个MD5脚本,它允许我传入一个arraybuffer并解决它。

Or at least, gave me the desired result, Sounds like there is just a lot more trial and error with javascript as everything is handled a bit differently to other languages. 或者至少,给了我想要的结果,听起来像javascript有更多的试验和错误,因为一切都处理与其他语言有点不同。

Thanks again. 再次感谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM