简体   繁体   English

使用javascript和PHP生成相同的MD5

[英]Generate the same MD5 using javascript and PHP

I am trying to build an application that needs to compare the MD5 hash of any file. 我正在尝试构建一个需要比较任何文件的MD5哈希的应用程序。 Due to specific issues, before the upload, the MD5 must be generated client side, and after the upload the application needs to check it server side. 由于具体问题,在上传之前,MD5必须是客户端生成的,上传后应用程序需要检查服务器端。

My first approach was to use, at the client side, the JavaScript File API and the FileReader.ReadAs functions. 我的第一种方法是在客户端使用JavaScript File API和FileReader.ReadAs函数。 Then I use the MD5 algorithm found here: http://pajhome.org.uk/crypt/md5/ 然后我使用这里找到的MD5算法: http//pajhome.org.uk/crypt/md5/

Server side, I would use PHP's fopen command and the md5 function. 服务器端,我会使用PHP的fopen命令和md5函数。

This approach works fine when using simple text files. 使用简单的文本文件时,此方法可正常工作。 But, when a binary file is used (like some jpg or pdf), the MD5 generated at the client side is different from the server. 但是,当使用二进制文件(如某些jpg或pdf)时,客户端生成的MD5与服务器不同。 Using md5sum command-line tool I figured out that the server MD5 is correct and the problem occurs at client side. 使用md5sum命令行工具我发现服务器MD5是正确的,问题发生在客户端。

I've tried other MD5 API's I found with the same results. 我已经尝试了其他MD5 API,我找到了相同的结果。 I suspect that FileReader.ReadAs functions are loading the file content slightly differently (I have tried all ReadAs function variants: text, binary and so on), but I can't figure out what is the difference. 我怀疑FileReader.ReadAs函数正在以稍微不同的方式加载文件内容(我已经尝试了所有的ReadAs函数变体:text,binary等),但我无法弄清楚有什么区别。

I'm missing something but don't know what, maybe I need to decode the content somehow before generating the MD5. 我遗漏了一些东西,但不知道是什么,也许我需要在生成MD5之前以某种方式解码内容。

Any tips? 有小费吗?

Edit 1: 编辑1:

I followed the idea given by optima1. 我遵循了optima1给出的想法。 Took each character and printed the unicode number both on javascript and PHP. 取出每个字符并在javascript和PHP上打印unicode编号。 I could see only one difference at the end on all the cases (used vimdiff). 在所有情况下我都看到最后只有一个区别(使用了vimdiff)。

PHP: 54 51 10 37 37 69 79 70 0 PHP:54 51 10 37 37 69 79 70 0

Javascript: 54 51 10 37 37 69 79 70 Javascript:54 51 10 37 37 69 79 70

Maybe this extra zero at PHP is some kind of "string end". 也许在PHP上这个额外的零点是某种“字符串结束”。 On both cases the binary strings have the same length. 在这两种情况下,二进制字符串具有相同的长度。 Adding a String.fromCharCode(0) to the end of the JS content do not solve the problem. 将String.fromCharCode(0)添加到JS内容的末尾并不能解决问题。 I will keep investigating. 我会继续调查。

If i can't find a solution i will try to build a giant string by concatenating those charcodes and using it to build the MD5. 如果我找不到解决方案,我将尝试通过连接这些字符并使用它来构建MD5来构建一个巨大的字符串。 It is a crap solution but will serve for now and i will just need to add a zero to the end of the JS string... 这是一个垃圾解决方案,但现在将服务,我只需要在JS字符串的末尾添加一个零...

Edit 2: 编辑2:

Thank God! 感谢上帝! This implementantion works like a charm: http://www.myersdaily.org/joseph/javascript/md5.js 这个实现就像一个魅力: http//www.myersdaily.org/joseph/javascript/md5.js

If you need to generate a MD5 hash from binary files, go for it. 如果需要从二进制文件生成MD5哈希,请选择它。

Thanks in advance! 提前致谢!

http://membres-liglab.imag.fr/donsez/cours/exemplescourstechnoweb/js_securehash/ http://membres-liglab.imag.fr/donsez/cours/exemplescourstechnoweb/js_securehash/

javascript md5 and php md5 both are same but we need to use some functions...that functions we can get from above url.... javascript md5和php md5都是一样的,但是我们需要使用一些函数...我们可以从上面的url获取的函数....

I would suggest doing a quick sanity check: have your client-side code report the first and last bytes of the binary data. 我建议快速进行健全性检查:让客户端代码报告二进制数据的第一个和最后一个字节。 Repeat in your PHP code. 在PHP代码中重复一遍。 Compare first and last bytes from both methods to ensure that they are in fact reading the same data (which should result in the same MD5 hash.) 比较两种方法的第一个和最后一个字节,以确保它们实际上读取相同的数据(这应该导致相同的MD5散列。)

Then I would suggest posting code here so that we can review. 然后我建议在这里发布代码,以便我们进行审核。

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

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