CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1

    HTML5 / Javascript File API - Attempting to find MD5 of the file

    Hi,

    I'm attempting to get a MD5 hash of a file by reading it in through the HTML 5.0 File API. I'm doing something wrong as I can read the file in and get a hash but it isn't the same as the hash i'd get from PHP MD5() function, and another program I have to compute hashes on files.

    Here is the code:

    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="utf-8" />
    	<title>MD5 Test</title>
    </head>
    
    <body id="index" class="home">
    
    	<h1>MD5 Test</h1>
    
    	<input type="file" id="files" name="files[]" multiple />
    	<output id="list"></output>
    
    	<!--from http://pajhome.org.uk/crypt/md5/ -->
    	<script type="text/javascript" src="md5-min.js"></script>
    	<script>
    	
    		function handleFileSelect(evt) 
    		{
    			var files = evt.target.files; // FileList object
    
    			// Loop through the FileList and render image files as thumbnails.
    			for (var i = 0, f; f = files[i]; i++) 
    			{
    				var reader = new FileReader();
    
    				// Closure to capture the file information.
    				reader.onload = (function(theFile) 
    				{
    					return function(e) 
    					{
    						var span = document.createElement('span');
    						var test = e.target.result;
    						var hash = hex_md5(test);
    						span.innerHTML = ['<br/><br/><b>Client side hashing function: </b>', hash].join('');
    						document.getElementById('list').insertBefore(span, null);
    					};
    					
    				})(f);
    
    				// Read in the image file as a data URL.
    				reader.readAsBinaryString(f);
    				
    				// Are these more appropriate file reading mechanisms?
    				//reader.readAsDataURL(f);
    				//reader.readAsArrayBuffer(f);
    				//reader.readAsText(f);
    			}
    		}
    
    		document.getElementById('files').addEventListener('change', handleFileSelect, false);
    	  
    	</script>
    
    </body>
    I think I understand why readAsBinaryString() is incorrect, and think readAsArrayBuffer() might be what I'm looking for (?) but couldn't figure out how to access the data in the ArrayBuffer. Perhaps someone could help me understand the data structure I have after calling one of these read mechanisms.

    Any help appreciated.
    Last edited by goatslayer; April 15th, 2011 at 05:06 AM.

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: HTML5 / Javascript File API - Attempting to find MD5 of the file

    PHP's md5() calculates the hash of a string, not a file. You want md5_file().
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3

    Re: HTML5 / Javascript File API - Attempting to find MD5 of the file

    Hi Peej,

    I might have been doing it a slightly odd way but reading the file using file_get_contents() I get identical results.

    Code:
    $file = file_get_contents('test.gif');
    echo '<br/><br/><b>PHP MD5 file hash of file: </b>' . md5($file);
    Is equivalent to:

    Code:
    echo '<br/><br/><b>PHP MD5 file hash of file: </b>' . md5_file('test.gif');
    Thanks for the information though, I can use md5_file() in future.

    Any ideas on where I'm going wrong on the client side?

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: HTML5 / Javascript File API - Attempting to find MD5 of the file

    I tried his example page. That doesn't give the same either.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Jul 2011
    Posts
    1

    Re: HTML5 / Javascript File API - Attempting to find MD5 of the file

    Did you ever figure it out? (nice to let people know if you do)

    Check out: http://phpjs.org/functions/md5:469

  6. #6

    Re: HTML5 / Javascript File API - Attempting to find MD5 of the file

    I'm afraid not yet. I'll have to revisit it but if anyone has suggestions I'd be glad to hear them.

    Thanks for the link to the MD5 library. I think the library is working as expected the problem is how I'm choosing to read the file.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured