pad-iso10126.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. ;(function (root, factory, undef) {
  2. if (typeof exports === "object") {
  3. // CommonJS
  4. module.exports = exports = factory(require("./core"), require("./cipher-core"));
  5. }
  6. else if (typeof define === "function" && define.amd) {
  7. // AMD
  8. define(["./core", "./cipher-core"], factory);
  9. }
  10. else {
  11. // Global (browser)
  12. factory(root.CryptoJS);
  13. }
  14. }(this, function (CryptoJS) {
  15. /**
  16. * ISO 10126 padding strategy.
  17. */
  18. CryptoJS.pad.Iso10126 = {
  19. pad: function (data, blockSize) {
  20. // Shortcut
  21. var blockSizeBytes = blockSize * 4;
  22. // Count padding bytes
  23. var nPaddingBytes = blockSizeBytes - data.sigBytes % blockSizeBytes;
  24. // Pad
  25. data.concat(CryptoJS.lib.WordArray.random(nPaddingBytes - 1)).
  26. concat(CryptoJS.lib.WordArray.create([nPaddingBytes << 24], 1));
  27. },
  28. unpad: function (data) {
  29. // Get number of padding bytes from last byte
  30. var nPaddingBytes = data.words[(data.sigBytes - 1) >>> 2] & 0xff;
  31. // Remove padding
  32. data.sigBytes -= nPaddingBytes;
  33. }
  34. };
  35. return CryptoJS.pad.Iso10126;
  36. }));