pkcs7.cjs.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*! @name pkcs7 @version 1.0.4 @license Apache-2.0 */
  2. 'use strict';
  3. Object.defineProperty(exports, '__esModule', { value: true });
  4. /*
  5. * pkcs7.pad
  6. * https://github.com/brightcove/pkcs7
  7. *
  8. * Copyright (c) 2014 Brightcove
  9. * Licensed under the apache2 license.
  10. */
  11. var PADDING;
  12. /**
  13. * Returns a new Uint8Array that is padded with PKCS#7 padding.
  14. *
  15. * @param plaintext {Uint8Array} the input bytes before encryption
  16. * @return {Uint8Array} the padded bytes
  17. * @see http://tools.ietf.org/html/rfc5652
  18. */
  19. function pad(plaintext) {
  20. var padding = PADDING[plaintext.byteLength % 16 || 0];
  21. var result = new Uint8Array(plaintext.byteLength + padding.length);
  22. result.set(plaintext);
  23. result.set(padding, plaintext.byteLength);
  24. return result;
  25. } // pre-define the padding values
  26. 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]];
  27. /**
  28. * Returns the subarray of a Uint8Array without PKCS#7 padding.
  29. *
  30. * @param padded {Uint8Array} unencrypted bytes that have been padded
  31. * @return {Uint8Array} the unpadded bytes
  32. * @see http://tools.ietf.org/html/rfc5652
  33. */
  34. function unpad(padded) {
  35. return padded.subarray(0, padded.byteLength - padded[padded.byteLength - 1]);
  36. }
  37. var version = "1.0.4";
  38. exports.VERSION = version;
  39. exports.pad = pad;
  40. exports.unpad = unpad;