|
| 1 | +var test = require('tape') |
| 2 | +var CipherBase = require('./') |
| 3 | +var inherits = require('inherits') |
| 4 | + |
| 5 | +test('basic version', function (t) { |
| 6 | + inherits(Cipher, CipherBase) |
| 7 | + function Cipher () { |
| 8 | + CipherBase.call(this) |
| 9 | + } |
| 10 | + Cipher.prototype._update = function (input) { |
| 11 | + t.ok(Buffer.isBuffer(input)) |
| 12 | + return input |
| 13 | + } |
| 14 | + Cipher.prototype._final = function () { |
| 15 | + // noop |
| 16 | + } |
| 17 | + var cipher = new Cipher() |
| 18 | + var utf8 = 'abc123abcd' |
| 19 | + var update = cipher.update(utf8, 'utf8', 'base64') + cipher.final('base64') |
| 20 | + var string = (new Buffer(update, 'base64')).toString() |
| 21 | + t.equals(utf8, string) |
| 22 | + t.end() |
| 23 | +}) |
| 24 | +test('hash mode', function (t) { |
| 25 | + inherits(Cipher, CipherBase) |
| 26 | + function Cipher () { |
| 27 | + CipherBase.call(this, 'finalName') |
| 28 | + this._cache = [] |
| 29 | + } |
| 30 | + Cipher.prototype._update = function (input) { |
| 31 | + t.ok(Buffer.isBuffer(input)) |
| 32 | + this._cache.push(input) |
| 33 | + } |
| 34 | + Cipher.prototype._final = function () { |
| 35 | + return Buffer.concat(this._cache) |
| 36 | + } |
| 37 | + var cipher = new Cipher() |
| 38 | + var utf8 = 'abc123abcd' |
| 39 | + var update = cipher.update(utf8, 'utf8').finalName('base64') |
| 40 | + var string = (new Buffer(update, 'base64')).toString() |
| 41 | + |
| 42 | + t.equals(utf8, string) |
| 43 | + t.end() |
| 44 | +}) |
| 45 | +test('encodings', function (t) { |
| 46 | + inherits(Cipher, CipherBase) |
| 47 | + function Cipher () { |
| 48 | + CipherBase.call(this) |
| 49 | + } |
| 50 | + Cipher.prototype._update = function (input) { |
| 51 | + return input |
| 52 | + } |
| 53 | + Cipher.prototype._final = function () { |
| 54 | + // noop |
| 55 | + } |
| 56 | + t.test('mix and match encoding', function (t) { |
| 57 | + t.plan(2) |
| 58 | + |
| 59 | + var cipher = new Cipher() |
| 60 | + cipher.update('foo', 'utf8', 'utf8') |
| 61 | + t.throws(function () { |
| 62 | + cipher.update('foo', 'utf8', 'base64') |
| 63 | + }) |
| 64 | + cipher = new Cipher() |
| 65 | + cipher.update('foo', 'utf8', 'base64') |
| 66 | + t.doesNotThrow(function () { |
| 67 | + cipher.update('foo', 'utf8') |
| 68 | + cipher.final('base64') |
| 69 | + }) |
| 70 | + }) |
| 71 | + t.test('handle long uft8 plaintexts', function (t) { |
| 72 | + t.plan(1) |
| 73 | + var txt = 'ふっかつ あきる すぶり はやい つける まゆげ たんさん みんぞく ねほりはほり せまい たいまつばな ひはん' |
| 74 | + |
| 75 | + var cipher = new Cipher() |
| 76 | + var decipher = new Cipher() |
| 77 | + var enc = decipher.update(cipher.update(txt, 'utf8', 'base64'), 'base64', 'utf8') |
| 78 | + enc += decipher.update(cipher.final('base64'), 'base64', 'utf8') |
| 79 | + enc += decipher.final('utf8') |
| 80 | + |
| 81 | + t.equals(txt, enc) |
| 82 | + }) |
| 83 | +}) |
0 commit comments