index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. 'use strict';
  2. var Buffer = require('safe-buffer').Buffer;
  3. var CipherBase = require('../');
  4. var test = require('tape');
  5. var inherits = require('inherits');
  6. test('basic version', function (t) {
  7. function Cipher() {
  8. CipherBase.call(this);
  9. }
  10. inherits(Cipher, CipherBase);
  11. Cipher.prototype._update = function (input) {
  12. t.ok(Buffer.isBuffer(input));
  13. return input;
  14. };
  15. Cipher.prototype._final = function () {
  16. // noop
  17. };
  18. var cipher = new Cipher();
  19. var utf8 = 'abc123abcd';
  20. var update = cipher.update(utf8, 'utf8', 'base64') + cipher['final']('base64');
  21. var string = Buffer.from(update, 'base64').toString();
  22. t.equals(utf8, string);
  23. t.end();
  24. });
  25. test('hash mode', function (t) {
  26. function Cipher() {
  27. CipherBase.call(this, 'finalName');
  28. this._cache = [];
  29. }
  30. inherits(Cipher, CipherBase);
  31. Cipher.prototype._update = function (input) {
  32. t.ok(Buffer.isBuffer(input));
  33. this._cache.push(input);
  34. };
  35. Cipher.prototype._final = function () {
  36. return Buffer.concat(this._cache);
  37. };
  38. var cipher = new Cipher();
  39. var utf8 = 'abc123abcd';
  40. var update = cipher.update(utf8, 'utf8').finalName('base64');
  41. var string = Buffer.from(update, 'base64').toString();
  42. t.equals(utf8, string);
  43. t.end();
  44. });
  45. test('hash mode as stream', function (t) {
  46. function Cipher() {
  47. CipherBase.call(this, 'finalName');
  48. this._cache = [];
  49. }
  50. inherits(Cipher, CipherBase);
  51. Cipher.prototype._update = function (input) {
  52. t.ok(Buffer.isBuffer(input));
  53. this._cache.push(input);
  54. };
  55. Cipher.prototype._final = function () {
  56. return Buffer.concat(this._cache);
  57. };
  58. var cipher = new Cipher();
  59. cipher.on('error', function (e) {
  60. t.notOk(e);
  61. });
  62. var utf8 = 'abc123abcd';
  63. cipher.end(utf8, 'utf8');
  64. var update = cipher.read().toString('base64');
  65. var string = Buffer.from(update, 'base64').toString();
  66. t.equals(utf8, string);
  67. t.end();
  68. });
  69. test('encodings', function (t) {
  70. function Cipher() {
  71. CipherBase.call(this);
  72. }
  73. inherits(Cipher, CipherBase);
  74. Cipher.prototype._update = function (input) {
  75. return input;
  76. };
  77. Cipher.prototype._final = function () {
  78. // noop
  79. };
  80. t.test('mix and match encoding', function (st) {
  81. st.plan(2);
  82. var cipher = new Cipher();
  83. cipher.update('foo', 'utf8', 'utf8');
  84. st['throws'](function () {
  85. cipher.update('foo', 'utf8', 'base64');
  86. });
  87. cipher = new Cipher();
  88. cipher.update('foo', 'utf8', 'base64');
  89. st.doesNotThrow(function () {
  90. cipher.update('foo', 'utf8');
  91. cipher['final']('base64');
  92. });
  93. });
  94. t.test('handle long uft8 plaintexts', function (st) {
  95. st.plan(1);
  96. var txt = 'ふっかつ あきる すぶり はやい つける まゆげ たんさん みんぞく ねほりはほり せまい たいまつばな ひはん';
  97. var cipher = new Cipher();
  98. var decipher = new Cipher();
  99. var enc = decipher.update(cipher.update(txt, 'utf8', 'base64'), 'base64', 'utf8');
  100. enc += decipher.update(cipher['final']('base64'), 'base64', 'utf8');
  101. enc += decipher['final']('utf8');
  102. st.equals(txt, enc);
  103. });
  104. });
  105. test('handle SafeBuffer instances', function (t) {
  106. function Cipher() {
  107. CipherBase.call(this, 'finalName');
  108. this._cache = [];
  109. }
  110. inherits(Cipher, CipherBase);
  111. Cipher.prototype._update = function (input) {
  112. t.ok(Buffer.isBuffer(input));
  113. this._cache.push(input);
  114. };
  115. Cipher.prototype._final = function () {
  116. return Buffer.concat(this._cache);
  117. };
  118. var cipher = new Cipher();
  119. var final = cipher.update(Buffer.from('a0c1', 'hex')).finalName('hex');
  120. t.equals(final, 'a0c1');
  121. t.end();
  122. });
  123. test('handle Uint8Array view', function (t) {
  124. function Cipher() {
  125. CipherBase.call(this, 'finalName');
  126. this._cache = [];
  127. }
  128. inherits(Cipher, CipherBase);
  129. Cipher.prototype._update = function (input) {
  130. t.ok(Buffer.isBuffer(input));
  131. this._cache.push(input);
  132. };
  133. Cipher.prototype._final = function () {
  134. return Buffer.concat(this._cache);
  135. };
  136. var buf = new Uint8Array([0, 1, 2, 3, 4, 5]);
  137. var uarr = new Uint8Array(buf.buffer, 2, 3);
  138. var cipher = new Cipher();
  139. var final = cipher.update(uarr).finalName('hex');
  140. t.equals(final, '020304');
  141. t.end();
  142. });
  143. test('handle empty Uint8Array instances', function (t) {
  144. function Cipher() {
  145. CipherBase.call(this, 'finalName');
  146. this._cache = [];
  147. }
  148. inherits(Cipher, CipherBase);
  149. Cipher.prototype._update = function (input) {
  150. t.ok(Buffer.isBuffer(input));
  151. this._cache.push(input);
  152. };
  153. Cipher.prototype._final = function () {
  154. return Buffer.concat(this._cache);
  155. };
  156. var cipher = new Cipher();
  157. var final = cipher.update(new Uint8Array(0)).finalName('hex');
  158. t.equals(final, '');
  159. t.end();
  160. });
  161. test('handle UInt16Array', function (t) {
  162. function Cipher() {
  163. CipherBase.call(this, 'finalName');
  164. this._cache = [];
  165. }
  166. inherits(Cipher, CipherBase);
  167. Cipher.prototype._update = function (input) {
  168. t.ok(Buffer.isBuffer(input));
  169. this._cache.push(input);
  170. };
  171. Cipher.prototype._final = function () {
  172. return Buffer.concat(this._cache);
  173. };
  174. if (ArrayBuffer.isView && (Buffer.prototype instanceof Uint8Array || Buffer.TYPED_ARRAY_SUPPORT)) {
  175. var cipher = new Cipher();
  176. var final = cipher.update(new Uint16Array([1234, 512])).finalName('hex');
  177. t.equals(final, 'd2040002');
  178. } else {
  179. t.skip('ArrayBuffer.isView and/or TypedArray not fully supported');
  180. }
  181. t.end();
  182. });