TomPlum / crypto-js

Automatically exported from code.google.com/p/crypto-js
1 stars 0 forks source link

Support Javascript Typed Arrays #46

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
It would be nice if it were possible to interface to CryptoJS using Javascript 
Typed Arrays.

A fairly low effort solution would be to allow WordArray to accept an 
ArrayBuffer as input and provide a conversion to ArrayBuffer as output.  Would 
there be support for this addition if a patch were supplied to implement it?

Original issue reported on code.google.com by brye...@chromium.org on 22 Aug 2012 at 9:18

GoogleCodeExporter commented 9 years ago
To do that, I would probably extend WordArray to create a new TypedWordArray 
type that we could customize. And I'd probably keep that definition in a 
separate component file rather than in the core since it wouldn't be 
cross-browser.

In fact, since JavaScript is loosely typed, you should be able to pass 
WordArray a typed array, and it would just use it and continue on as normal. 
You may run into problems, however, when the library tries to use the standard 
array methods, such as push, slice and splice. Something to keep in mind if you 
decide to toy with the idea.

Original comment by Jeff.Mott.OR on 22 Aug 2012 at 10:00

GoogleCodeExporter commented 9 years ago

Original comment by Jeff.Mott.OR on 22 Aug 2012 at 10:00

GoogleCodeExporter commented 9 years ago

Original comment by Jeff.Mott.OR on 22 Aug 2012 at 10:01

GoogleCodeExporter commented 9 years ago

Original comment by Jeff.Mott.OR on 22 Aug 2012 at 10:02

GoogleCodeExporter commented 9 years ago
I like the second idea of just having WordArray be able to accept an 
ArrayBuffer.  That seems like the most natural fit for users of the library.

However, my thinking was to actually extract the data out of the ArrayBuffer, 
into the WordArray words member.  Since this member is accessed directly in the 
library, this seems like the only real option without significant surgery.

Doing the ArrayBuffer <-> WordArray.words conversions won't be particularly 
fast, but they'll at least be convenient for users of the service.

Thoughts?

Original comment by brye...@chromium.org on 23 Aug 2012 at 12:54

GoogleCodeExporter commented 9 years ago
The latest release includes the ability to create WordArrays from typed arrays. 
You'll need to include an additional component -- lib-typedarrays.

<script 
src="http://crypto-js.googlecode.com/svn/tags/3.1/build/components/core.js"></sc
ript>
<script 
src="http://crypto-js.googlecode.com/svn/tags/3.1/build/components/lib-typedarra
ys.js"></script>
<script>
    var wordArray = CryptoJS.lib.WordArray.create(uint8View);
</script>

You can create a WordArray from any of the typed array views -- Int8Array, 
Int16Array, etc. -- or from an ArrayBuffer, or from a DataView.

Original comment by Jeff.Mott.OR on 7 Jan 2013 at 1:55

GoogleCodeExporter commented 9 years ago
This is awesome!  Thank you.

Original comment by brye...@chromium.org on 7 Jan 2013 at 6:46