pkcs7.pad.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536
  1. /*! @name pkcs7 @version 1.0.4 @license Apache-2.0 */
  2. (function (global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  4. typeof define === 'function' && define.amd ? define(factory) :
  5. (global = global || self, (global.pkcs7 = global.pkcs7 || {}, global.pkcs7.pad = factory()));
  6. }(this, function () { 'use strict';
  7. /*
  8. * pkcs7.pad
  9. * https://github.com/brightcove/pkcs7
  10. *
  11. * Copyright (c) 2014 Brightcove
  12. * Licensed under the apache2 license.
  13. */
  14. var PADDING;
  15. /**
  16. * Returns a new Uint8Array that is padded with PKCS#7 padding.
  17. *
  18. * @param plaintext {Uint8Array} the input bytes before encryption
  19. * @return {Uint8Array} the padded bytes
  20. * @see http://tools.ietf.org/html/rfc5652
  21. */
  22. function pad(plaintext) {
  23. var padding = PADDING[plaintext.byteLength % 16 || 0];
  24. var result = new Uint8Array(plaintext.byteLength + padding.length);
  25. result.set(plaintext);
  26. result.set(padding, plaintext.byteLength);
  27. return result;
  28. } // pre-define the padding values
  29. PADDING = [[16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16], [15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15], [14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14], [13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13], [12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12], [11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11], [10, 10, 10, 10, 10, 10, 10, 10, 10, 10], [9, 9, 9, 9, 9, 9, 9, 9, 9], [8, 8, 8, 8, 8, 8, 8, 8], [7, 7, 7, 7, 7, 7, 7], [6, 6, 6, 6, 6, 6], [5, 5, 5, 5, 5], [4, 4, 4, 4], [3, 3, 3], [2, 2], [1]];
  30. return pad;
  31. }));