1

I have the following snippet

    function runUpload( file ) {
        key_name = file.name
        if( file.type === 'image/png'  || 
                file.type === 'image/jpg'  || 
            file.type === 'image/jpeg' ||
                file.type === 'image/gif'  ||
                file.type === 'image/bmp'  ){
            var reader = new FileReader(),
                    image = new Image();
            reader.readAsDataURL( file );
            reader.onload = function( _file ){
                keyhash = md5(atob(_file.target.result.split(',')[1]));
                ZZ('key').el.src = _file.target.result;
                ZZ('key').el.style.display = 'block';
            } 
        } 
    }

The reason that the split happens is because if I left it at just:

_file.target.result

The resulting string would be:

data:text/plain;base64,SEVMTE8=

It uses https://raw.githubusercontent.com/blueimp/JavaScript-MD5/master/js/md5.min.js

The value of keyhash is different from when I do:

cat file.png | md5sum

Also when doing the python below, it gives me a different value to the JS version but the same as Linux

import hashlib

def md5Checksum(filePath):
    with open(filePath, 'rb') as fh:
        m = hashlib.md5()
        while True:
            data = fh.read(8192)
            if not data:
                break
            m.update(data)
        return m.hexdigest()

print('The MD5 checksum of text.txt is', md5Checksum('000000.png'))

Am I missing something obvious or should they all return the same value?

JS: dc839221df178f70c08fa510a3d76a3b
Python and MD5SUM: 9310dad66d4a73e29a761fb724f2825

In fact, if I have a file called HELLO.txt and the only content in there is: HELLO

base64'ing it in Linux/Python give the same result which is different to when JS does it.

SEVMTE8K vs SEVMTE8=

I think it may be something to do with new lines, break likes, return carriages etc

JavaScript has it as one continuous stream whereas editing the file in an text editor shows line breaks

Thanks

pee2pee
  • 3,619
  • 7
  • 52
  • 133
  • 1
    `_file.target.result.split(',')[1]`, what is this for? – Frontear Dec 12 '19 at 14:19
  • 1
    The Javascript function brings back data:image/png;base64 (or similar) at the start and then the content of the file, all of which is separated by the comma – pee2pee Dec 12 '19 at 15:02

1 Answers1

0

The only explanation is that encoding is different. Figure out encoding in JS (ISO-8859-1) to that in python (UTF-8).

Phani
  • 13
  • 3