index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. 'use strict';
  2. var Buffer = require('safe-buffer').Buffer;
  3. var Transform = require('stream').Transform;
  4. var StringDecoder = require('string_decoder').StringDecoder;
  5. var inherits = require('inherits');
  6. function CipherBase(hashMode) {
  7. Transform.call(this);
  8. this.hashMode = typeof hashMode === 'string';
  9. if (this.hashMode) {
  10. this[hashMode] = this._finalOrDigest;
  11. } else {
  12. this['final'] = this._finalOrDigest;
  13. }
  14. if (this._final) {
  15. this.__final = this._final;
  16. this._final = null;
  17. }
  18. this._decoder = null;
  19. this._encoding = null;
  20. }
  21. inherits(CipherBase, Transform);
  22. var useUint8Array = typeof Uint8Array !== 'undefined';
  23. var useArrayBuffer = typeof ArrayBuffer !== 'undefined'
  24. && typeof Uint8Array !== 'undefined'
  25. && ArrayBuffer.isView
  26. && (Buffer.prototype instanceof Uint8Array || Buffer.TYPED_ARRAY_SUPPORT);
  27. function toBuffer(data, encoding) {
  28. /*
  29. * No need to do anything for exact instance
  30. * This is only valid when safe-buffer.Buffer === buffer.Buffer, i.e. when Buffer.from/Buffer.alloc existed
  31. */
  32. if (data instanceof Buffer) {
  33. return data;
  34. }
  35. // Convert strings to Buffer
  36. if (typeof data === 'string') {
  37. return Buffer.from(data, encoding);
  38. }
  39. /*
  40. * Wrap any TypedArray instances and DataViews
  41. * Makes sense only on engines with full TypedArray support -- let Buffer detect that
  42. */
  43. if (useArrayBuffer && ArrayBuffer.isView(data)) {
  44. // Bug in Node.js <6.3.1, which treats this as out-of-bounds
  45. if (data.byteLength === 0) {
  46. return Buffer.alloc(0);
  47. }
  48. var res = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
  49. /*
  50. * Recheck result size, as offset/length doesn't work on Node.js <5.10
  51. * We just go to Uint8Array case if this fails
  52. */
  53. if (res.byteLength === data.byteLength) {
  54. return res;
  55. }
  56. }
  57. /*
  58. * Uint8Array in engines where Buffer.from might not work with ArrayBuffer, just copy over
  59. * Doesn't make sense with other TypedArray instances
  60. */
  61. if (useUint8Array && data instanceof Uint8Array) {
  62. return Buffer.from(data);
  63. }
  64. /*
  65. * Old Buffer polyfill on an engine that doesn't have TypedArray support
  66. * Also, this is from a different Buffer polyfill implementation then we have, as instanceof check failed
  67. * Convert to our current Buffer implementation
  68. */
  69. if (
  70. Buffer.isBuffer(data)
  71. && data.constructor
  72. && typeof data.constructor.isBuffer === 'function'
  73. && data.constructor.isBuffer(data)
  74. ) {
  75. return Buffer.from(data);
  76. }
  77. throw new TypeError('The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView.');
  78. }
  79. CipherBase.prototype.update = function (data, inputEnc, outputEnc) {
  80. var bufferData = toBuffer(data, inputEnc); // asserts correct input type
  81. var outData = this._update(bufferData);
  82. if (this.hashMode) {
  83. return this;
  84. }
  85. if (outputEnc) {
  86. outData = this._toString(outData, outputEnc);
  87. }
  88. return outData;
  89. };
  90. CipherBase.prototype.setAutoPadding = function () {};
  91. CipherBase.prototype.getAuthTag = function () {
  92. throw new Error('trying to get auth tag in unsupported state');
  93. };
  94. CipherBase.prototype.setAuthTag = function () {
  95. throw new Error('trying to set auth tag in unsupported state');
  96. };
  97. CipherBase.prototype.setAAD = function () {
  98. throw new Error('trying to set aad in unsupported state');
  99. };
  100. CipherBase.prototype._transform = function (data, _, next) {
  101. var err;
  102. try {
  103. if (this.hashMode) {
  104. this._update(data);
  105. } else {
  106. this.push(this._update(data));
  107. }
  108. } catch (e) {
  109. err = e;
  110. } finally {
  111. next(err);
  112. }
  113. };
  114. CipherBase.prototype._flush = function (done) {
  115. var err;
  116. try {
  117. this.push(this.__final());
  118. } catch (e) {
  119. err = e;
  120. }
  121. done(err);
  122. };
  123. CipherBase.prototype._finalOrDigest = function (outputEnc) {
  124. var outData = this.__final() || Buffer.alloc(0);
  125. if (outputEnc) {
  126. outData = this._toString(outData, outputEnc, true);
  127. }
  128. return outData;
  129. };
  130. CipherBase.prototype._toString = function (value, enc, fin) {
  131. if (!this._decoder) {
  132. this._decoder = new StringDecoder(enc);
  133. this._encoding = enc;
  134. }
  135. if (this._encoding !== enc) {
  136. throw new Error('can’t switch encodings');
  137. }
  138. var out = this._decoder.write(value);
  139. if (fin) {
  140. out += this._decoder.end();
  141. }
  142. return out;
  143. };
  144. module.exports = CipherBase;