levitation / crypto-js

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

typed arrays support always use entire buffer if a view is passed in #63

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
With a typed array view, the .buffer refers to the entire buffer, whereas the 
view might only be referencing a portion of it.  That is:

  var b = new ArrayBuffer(12);
  var v = new Uint8Array(b, 4, 4); // creates a 4-byte view starting at index 4

at this point:

  v.length == 4
  v.buffer.byteLength == 12  (because it's the original created buffer, not a sub-buffer).

The lib-typedarrays code as written will hash the entire buffer instead of just 
the view.  Additionally, using DataView adds an unnecessary performance cost.  
Instead, something like this would both fix the problem and be faster:

if (arg instanceof ArrayBuffer) {
  dataBuffer = new Uint8Array(arg);
} else if (arg instanceof Int32Array || arg instanceof Uint32Array || ...) {
  dataBuffer = new Uint8Array(arg.buffer, arg.byteOffset, arg.byteLength);
}

...
   words[i >>> 2] |= dataBuffer[i] << (24 - (i % 4) * 8);
...

(I should've just written a patch, but didn't want to deal with svn! :)

A further optimization is possible in some cases by just creating a Uint32Array 
view, assuming that both the offset and length are 4-byte-aligned.  In that 
case, the words member could also possibly just be a Uint32Array (assuming no 
Array features are used on it).

Original issue reported on code.google.com by vladim...@gmail.com on 9 Jan 2013 at 7:13