2

I have a simple PHP file which loads a file from my server, base64 encodes it and echoes it out.

Then I have a simple HTML page that uses jQuery to fetch this file, base64 decode it and do a checksum test. The checksum test is not working.

I md5'd the file in PHP after encoding it and md5'd it in javascript before decoding it and the checksums matched (So nothing went wrong during transit). However, the pre encoding and post decoding checksums do NOT match.

I am using webtoolkit.base64.js for decoding it in JavaScript. The file is a binary file (A ZIP archive).

Is there a problem with the decoding library or something else I'm not aware of that could cause this issue? Could it be a problem with the MD5 library I'm using (http://pajhome.org.uk/crypt/md5/md5.html)

2
  • have you tried sending a simple text file, it would be easier to debug, and dont rule out that the base64 encode might be at fault. Commented Feb 23, 2012 at 16:39
  • I have done these tests with text files and it works fine (Including the checksum part). I'm assuming one of the two libraries I am using doesn't handle binary data well. Commented Feb 23, 2012 at 16:43

2 Answers 2

2

Summary
Your MD5 library is OK, your base64 library is broken.

Both your JavaScript Base64 library and MD5 library are not working correctly.

  1. I have created and verified a ZIP file of 15097 bytes.
    MD5 sum: a9de6b8e5a9173140cb46d4b3b31b67c
  2. I have base64-encoded this file: http://pastebin.com/2rfdTzYT (20132 bytes).
  3. Verify the length of the base64 file at pastebin, using the following JavaScript snippet:
    document.querySelector('.de1').textContent.replace(/\s/g,'').length;
  4. Base64-decode the file properly using atob, and verify the size:

    window.b64_str = document.querySelector('.de1').textContent.replace(/\s/g,'');
    console.log( atob(window.b64_str).length ); /* 15097 */
    
  5. I verified that both files were exactly equal using the Hexdump JavaScript library, and the xxd UNIX command (available as EXE file for Windows).

Using your Base64 decoder, I get a string with the size of 8094. That is not 15097! During my tests, I discovered that the atob method returned incorrect bytes after certain byte sequences, including carriage returns. I have not yet found a solution to this.

Your MD5 library is OK.

Sign up to request clarification or add additional context in comments.

1 Comment

I have created an own Hexdump library in the past day, and verified that atob is actually working fine. The md5sum algortihm of your library seems to be different than the one in UNIX/PHP. When I have more time, I might dig deeper in it.
0

I may be misunderstanding the question, but if I'm not I've run into something like this before. The javascript library you're using doesn't do binary. What php encodes is going to be a bunch of 1's and 0's but what the javascript spits out is going to be text. If you want a binary string you'll have to convert the resulting text to binary, then it should be the same as your original file.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.