pkcs7.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.pkcs7=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
  2. /*
  3. * pkcs7.pad
  4. * https://github.com/brightcove/pkcs7
  5. *
  6. * Copyright (c) 2014 Brightcove
  7. * Licensed under the apache2 license.
  8. */
  9. 'use strict';
  10. var PADDING;
  11. /**
  12. * Returns a new Uint8Array that is padded with PKCS#7 padding.
  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. module.exports = function pad(plaintext) {
  18. var padding = PADDING[(plaintext.byteLength % 16) || 0],
  19. result = new Uint8Array(plaintext.byteLength + padding.length);
  20. result.set(plaintext);
  21. result.set(padding, plaintext.byteLength);
  22. return result;
  23. };
  24. // pre-define the padding values
  25. PADDING = [
  26. [16, 16, 16, 16,
  27. 16, 16, 16, 16,
  28. 16, 16, 16, 16,
  29. 16, 16, 16, 16],
  30. [15, 15, 15, 15,
  31. 15, 15, 15, 15,
  32. 15, 15, 15, 15,
  33. 15, 15, 15],
  34. [14, 14, 14, 14,
  35. 14, 14, 14, 14,
  36. 14, 14, 14, 14,
  37. 14, 14],
  38. [13, 13, 13, 13,
  39. 13, 13, 13, 13,
  40. 13, 13, 13, 13,
  41. 13],
  42. [12, 12, 12, 12,
  43. 12, 12, 12, 12,
  44. 12, 12, 12, 12],
  45. [11, 11, 11, 11,
  46. 11, 11, 11, 11,
  47. 11, 11, 11],
  48. [10, 10, 10, 10,
  49. 10, 10, 10, 10,
  50. 10, 10],
  51. [9, 9, 9, 9,
  52. 9, 9, 9, 9,
  53. 9],
  54. [8, 8, 8, 8,
  55. 8, 8, 8, 8],
  56. [7, 7, 7, 7,
  57. 7, 7, 7],
  58. [6, 6, 6, 6,
  59. 6, 6],
  60. [5, 5, 5, 5,
  61. 5],
  62. [4, 4, 4, 4],
  63. [3, 3, 3],
  64. [2, 2],
  65. [1]
  66. ];
  67. },{}],2:[function(_dereq_,module,exports){
  68. /*
  69. * pkcs7
  70. * https://github.com/brightcove/pkcs7
  71. *
  72. * Copyright (c) 2014 Brightcove
  73. * Licensed under the apache2 license.
  74. */
  75. 'use strict';
  76. exports.pad = _dereq_('./pad.js');
  77. exports.unpad = _dereq_('./unpad.js');
  78. },{"./pad.js":1,"./unpad.js":3}],3:[function(_dereq_,module,exports){
  79. /*
  80. * pkcs7.unpad
  81. * https://github.com/brightcove/pkcs7
  82. *
  83. * Copyright (c) 2014 Brightcove
  84. * Licensed under the apache2 license.
  85. */
  86. 'use strict';
  87. /**
  88. * Returns the subarray of a Uint8Array without PKCS#7 padding.
  89. * @param padded {Uint8Array} unencrypted bytes that have been padded
  90. * @return {Uint8Array} the unpadded bytes
  91. * @see http://tools.ietf.org/html/rfc5652
  92. */
  93. module.exports = function unpad(padded) {
  94. return padded.subarray(0, padded.byteLength - padded[padded.byteLength - 1]);
  95. };
  96. },{}]},{},[2])
  97. (2)
  98. });