pad-ansix923.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. * ANSI X.923 padding strategy.
  15. */
  16. CryptoJS.pad.AnsiX923 = {
  17. pad: function (data, blockSize) {
  18. // Shortcuts
  19. var dataSigBytes = data.sigBytes;
  20. var blockSizeBytes = blockSize * 4;
  21. // Count padding bytes
  22. var nPaddingBytes = blockSizeBytes - dataSigBytes % blockSizeBytes;
  23. // Compute last byte position
  24. var lastBytePos = dataSigBytes + nPaddingBytes - 1;
  25. // Pad
  26. data.clamp();
  27. data.words[lastBytePos >>> 2] |= nPaddingBytes << (24 - (lastBytePos % 4) * 8);
  28. data.sigBytes += nPaddingBytes;
  29. },
  30. unpad: function (data) {
  31. // Get number of padding bytes from last byte
  32. var nPaddingBytes = data.words[(data.sigBytes - 1) >>> 2] & 0xff;
  33. // Remove padding
  34. data.sigBytes -= nPaddingBytes;
  35. }
  36. };
  37. return CryptoJS.pad.Ansix923;
  38. }));