使用javascript和PHP生成相同的MD5 [英] Generate the same MD5 using javascript and PHP

查看:67
本文介绍了使用javascript和PHP生成相同的MD5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个需要比较任何文件的MD5哈希值的应用程序. 由于特定问题,在上传之前,必须先在客户端生成MD5,在上传之后,应用程序需要在服务器端对其进行检查.

I am trying to build an application that needs to compare the MD5 hash of any file. 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.

我的第一种方法是在客户端使用JavaScript File API和FileReader.ReadAs函数.然后,我使用此处找到的MD5算法: http://pajhome.org.uk/crypt/md5/

My first approach was to use, at the client side, the JavaScript File API and the FileReader.ReadAs functions. Then I use the MD5 algorithm found here: http://pajhome.org.uk/crypt/md5/

在服务器端,我将使用PHP的fopen命令和md5函数.

Server side, I would use PHP's fopen command and the md5 function.

使用简单的文本文件时,此方法效果很好.但是,当使用二进制文件(如jpg或pdf)时,在客户端生成的MD5与服务器不同.使用md5sum命令行工具,我发现服务器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. Using md5sum command-line tool I figured out that the server MD5 is correct and the problem occurs at client side.

我尝试了发现的其他MD5 API,但结果相同.我怀疑FileReader.ReadAs函数加载文件内容的方式略有不同(我尝试了所有ReadAs函数变体:文本,二进制等),但是我不知道有什么区别.

I've tried other MD5 API's I found with the same results. 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.

我丢失了一些东西,但不知道是什么,也许我需要在生成MD5之前以某种方式对内容进行解码.

I'm missing something but don't know what, maybe I need to decode the content somehow before generating the MD5.

有什么提示吗?

我遵循了optima1给出的想法.抓住每个字符并在javascript和PHP上打印unicode号.最后,在所有情况下(使用vimdiff),我只能看到一个区别.

I followed the idea given by optima1. Took each character and printed the unicode number both on javascript and PHP. I could see only one difference at the end on all the cases (used 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

也许PHP上的这个额外的零是某种字符串结尾".在这两种情况下,二进制字符串的长度都相同.将String.fromCharCode(0)添加到JS内容的末尾并不能解决问题.我会继续调查.

Maybe this extra zero at PHP is some kind of "string end". 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. I will keep investigating.

如果找不到解决方案,我将尝试通过串联这些字符并使用它来构建MD5来构建一个巨大的字符串.这是一个废话解决方案,但现在可以使用,我只需要在JS字符串的末尾添加一个零即可.

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. 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...

谢谢上帝!此实现的工作原理很简单: http://www.myersdaily.org/joseph/javascript/md5.js

Thank God! This implementantion works like a charm: http://www.myersdaily.org/joseph/javascript/md5.js

如果您需要从二进制文件中生成MD5哈希,请继续使用

If you need to generate a MD5 hash from binary files, go for it.

提前谢谢!

推荐答案

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

javascript md5和php md5都相同,但是我们需要使用一些函数...可以从上面的url中获得的函数....

javascript md5 and php md5 both are same but we need to use some functions...that functions we can get from above url....

这篇关于使用javascript和PHP生成相同的MD5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆