pkcs7.es.js 1.6 KB

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