pad-iso10126.js 1.2 KB

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