JavaScript md5_file
Calculate the md5 hash of given filename
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 | function md5_file ( str_filename ) { // Calculate the md5 hash of given filename // // version: 909.322 // discuss at: http://phpjs.org/functions/md5_file // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + input by: Brett Zamir (http://brett-zamir.me) // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // - depends on: file_get_contents // - depends on: md5 // * example 1: md5_file('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm'); // * returns 1: '202cb962ac59075b964b07152d234b70' var buf = ''; buf = this.file_get_contents(str_filename); if (!buf) { return false; } return this.md5(buf); } |
Examples
Running
1 | md5_file('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm'); |
Should return
1 | '202cb962ac59075b964b07152d234b70' |
Dependencies
In order to use this function, you also need:
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have md5_file goodness in JavaScript.
In doing testing, we should also confirm whether md5() itself is giving correct results for the file represented as a string in PHP and our version...
Nope, not that either... This should be it sorry...
1
2
3
4
56
| function md5_file ( str_filename ) { var buf = ''; buf = this.file_get_contents(str_filename, 'FILE_BINARY'); buf = buf.replace(/[\s\S]/g, function (n) {return String.fromCharCode(n.charCodeAt(0) & 0xFF);}); return buf ? this.md5(buf) : false;} |
Argh, sorry, two mistakes in that one...Try this:
1
2
3
4
5 | function md5_file ( str_filename ) { var buf = ''; buf = this.file_get_contents(str_filename, 'FILE_BINARY'); return buf ? this.md5(buf).replace(/[\s\S]/g, function (n) {return n.charCodeAt(0) & 0xFF;}) : false; } |
@Robert: Could you check whether the following works? Make sure you have the latest version of file_get_contents() too. It might only work in Firefox (and not IE) though, since it relies on overrideMimeType()...
I think we need to get the file in binary form as PHP does (we currently don't by default because it is usually not as useful). But maybe we should though to reflect the PHP API...
Anyways, let us know if this approach works at least in FF... I think my regular expression is correct and necessary there to shift back everything from the private characters into the normal ASCII range...(the trick used inside file_get_contents() is documented at https://developer.mozilla.org/En/Using_XMLHttpRequest#Receiving_binary_data )
1
2
3
4
56
| function md5_file ( str_filename ) { var buf = ''; buf = this.file_get_contents(str_filename, 'FILE_BINARY'); buf = buf.replace(/[\s\S]/g, function (n) {n.charCodeAt(0) & 0xFF;}); return buf ? this.md5(buf) : false;} |
To update my last comment it is not creating the md5 of the file name but it is not creting an md5 which matches the md5sum in bash or md5_file in perl.
I have run your scripts and I have found a flaw the md5 that the scripts produce is of the file name not the file itself. The md5_file function in perl is the md5 of the file not the file name.


Brett Zamir
Jan 7th