pkcs7.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*! @name pkcs7 @version 1.0.4 @license Apache-2.0 */
  2. (function (global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  4. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  5. (global = global || self, factory(global.pkcs7 = {}));
  6. }(this, function (exports) { '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. /**
  31. * Returns the subarray of a Uint8Array without PKCS#7 padding.
  32. *
  33. * @param padded {Uint8Array} unencrypted bytes that have been padded
  34. * @return {Uint8Array} the unpadded bytes
  35. * @see http://tools.ietf.org/html/rfc5652
  36. */
  37. function unpad(padded) {
  38. return padded.subarray(0, padded.byteLength - padded[padded.byteLength - 1]);
  39. }
  40. var version = "1.0.4";
  41. exports.VERSION = version;
  42. exports.pad = pad;
  43. exports.unpad = unpad;
  44. Object.defineProperty(exports, '__esModule', { value: true });
  45. }));