Export to GitHub

crypto-js - issue #67

Support binary input types


Posted on Jan 22, 2013 by Quick Kangaroo

What steps will reproduce the problem? 1. Use the javascript ArrayBuffer interface as the input to computing an md5checksum.

What is the expected output? What do you see instead? I'd expect that the "update" interface can take either a string or typed arrays. Instead I get an error because the Uint32Array doesn't behave as a String.

What version of the product are you using? On what operating system? 3.1, Ubuntu.

Please provide any additional information below.

Comment #1

Posted on Jan 22, 2013 by Happy Horse

I think it's reasonable to expect update() to accept typed arrays. I'll likely change that. In the mean time, you'll have to do this:

md5.update( CryptoJS.lib.WordArray.create(typedArray) );

Comment #2

Posted on Jan 22, 2013 by Quick Kangaroo

Thanks for the quick reply Jeff. Also that WordArray workaround will be perfect! Is there also a workaround for Blob?

Thanks, Evan

Comment #3

Posted on Jan 22, 2013 by Quick Kangaroo

Actually, I don't think this works. Please see the attached file for an example using an ArrayBuffer. Can we change the issue type to bug? Is there any other way I can accomplish this? I'm desperately in need of a way to incrementally compute the MD5 checksum of a binary file in javascript. Thanks for your help!

Attachments

Comment #4

Posted on Jan 22, 2013 by Happy Horse

Ahh. I didn't document this very well... or, at all, for that matter... but typed array support comes in a separate component. So include...

<script src='md5-3.1.2.min.js'></script>
<script src='http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/lib-typedarrays-min.js'></script>

...then everything should just work.

Comment #5

Posted on Jan 22, 2013 by Quick Kangaroo

Ah, pefect! Thank you!

Comment #6

Posted on Jan 23, 2013 by Massive Panda

Is this also working with SHA1? I am trying to compute the SHA1 hash of an entire MP3 file in the browser, and I am getting hashes that do not match what I expect even after trying: CryptoJS.lib.WordArray.create

This file is loaded via a FileReader calling readAsBinaryString

Comment #7

Posted on Jan 23, 2013 by Massive Panda

Ah, I used readAsArrayBuffer instead and this method seems to be working now.

Is it difficult to handle "BinaryString"s ?

Comment #8

Posted on Jan 23, 2013 by Quick Kangaroo

Comment deleted

Comment #9

Posted on Jan 23, 2013 by Quick Kangaroo

Wolfgang, I'd suggest you stick with readAsArrayBuffer. FileReader.readAsBinaryString has been deprecated - https://developer.mozilla.org/en-US/docs/DOM/FileReaderSync#readAsBinaryString()_Deprecated.

Comment #10

Posted on Jan 23, 2013 by Massive Panda

Yeah, I liked the ArrayBuffer initially and realized it wasn't easily supported by crypto-js until I found this issue.

I guess it isn't deprecated for the FileReader itself? But I don't mind using ArrayBuffers :-)

Comment #11

Posted on Jan 23, 2013 by Quick Kangaroo

Indeed Jeff saved me when he showed me how to use ArrayBuffer. Also I do think readAsBinaryString has been removed from the spec, see http://www.w3.org/TR/FileAPI/, you won't find any mention of it.

Best, Evan

Comment #12

Posted on Jan 23, 2013 by Massive Panda

Very good point with a good canonical link :-) I'm new to Javascript and just playing around (mostly), but these APIs are a little insane (input from multiple parties, things deprecated all over).

Comment #13

Posted on Oct 10, 2013 by Massive Camel

Just for the record, I recently found an pretty old post on the Google Group with a snippet for building an ArrayBuffer (UInt8Array) from a WordArray. Here's the link:

https://groups.google.com/d/msg/crypto-js/TOb92tcJlU0/Eq7VZ5tpi-QJ

IMO, that would be an awesome addition to CryptoJS to have a full ArrayBuffer <-> ArrayBuffer flow.

Best

Comment #14

Posted on Jun 18, 2014 by Swift Kangaroo

Converting the typedArray to string/word array is nice to get things working, but it's really slow. Is it possible to implement the algorithms directly from the typedArray?

Status: New

Labels:
Type-Enhancement Priority-Medium