lib-typedarrays.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. CryptoJS v3.1.2
  3. code.google.com/p/crypto-js
  4. (c) 2009-2013 by Jeff Mott. All rights reserved.
  5. code.google.com/p/crypto-js/wiki/License
  6. */
  7. (function () {
  8. // Check if typed arrays are supported
  9. if (typeof ArrayBuffer != 'function') {
  10. return;
  11. }
  12. // Shortcuts
  13. var C = CryptoJS;
  14. var C_lib = C.lib;
  15. var WordArray = C_lib.WordArray;
  16. // Reference original init
  17. var superInit = WordArray.init;
  18. // Augment WordArray.init to handle typed arrays
  19. var subInit = WordArray.init = function (typedArray) {
  20. // Convert buffers to uint8
  21. if (typedArray instanceof ArrayBuffer) {
  22. typedArray = new Uint8Array(typedArray);
  23. }
  24. // Convert other array views to uint8
  25. if (
  26. typedArray instanceof Int8Array ||
  27. typedArray instanceof Uint8ClampedArray ||
  28. typedArray instanceof Int16Array ||
  29. typedArray instanceof Uint16Array ||
  30. typedArray instanceof Int32Array ||
  31. typedArray instanceof Uint32Array ||
  32. typedArray instanceof Float32Array ||
  33. typedArray instanceof Float64Array
  34. ) {
  35. typedArray = new Uint8Array(typedArray.buffer, typedArray.byteOffset, typedArray.byteLength);
  36. }
  37. // Handle Uint8Array
  38. if (typedArray instanceof Uint8Array) {
  39. // Shortcut
  40. var typedArrayByteLength = typedArray.byteLength;
  41. // Extract bytes
  42. var words = [];
  43. for (var i = 0; i < typedArrayByteLength; i++) {
  44. words[i >>> 2] |= typedArray[i] << (24 - (i % 4) * 8);
  45. }
  46. // Initialize this word array
  47. superInit.call(this, words, typedArrayByteLength);
  48. } else {
  49. // Else call normal init
  50. superInit.apply(this, arguments);
  51. }
  52. };
  53. subInit.prototype = WordArray;
  54. }());