cipher-base
Advanced tools
Comparing version 1.0.3 to 1.0.4
39
index.js
@@ -0,6 +1,6 @@ | ||
var Buffer = require('safe-buffer').Buffer | ||
var Transform = require('stream').Transform | ||
var StringDecoder = require('string_decoder').StringDecoder | ||
var inherits = require('inherits') | ||
var StringDecoder = require('string_decoder').StringDecoder | ||
module.exports = CipherBase | ||
inherits(CipherBase, Transform) | ||
function CipherBase (hashMode) { | ||
@@ -14,16 +14,23 @@ Transform.call(this) | ||
} | ||
if (this._final) { | ||
this.__final = this._final | ||
this._final = null | ||
} | ||
this._decoder = null | ||
this._encoding = null | ||
} | ||
inherits(CipherBase, Transform) | ||
CipherBase.prototype.update = function (data, inputEnc, outputEnc) { | ||
if (typeof data === 'string') { | ||
data = new Buffer(data, inputEnc) | ||
data = Buffer.from(data, inputEnc) | ||
} | ||
var outData = this._update(data) | ||
if (this.hashMode) { | ||
return this | ||
} | ||
if (this.hashMode) return this | ||
if (outputEnc) { | ||
outData = this._toString(outData, outputEnc) | ||
} | ||
return outData | ||
@@ -33,3 +40,2 @@ } | ||
CipherBase.prototype.setAutoPadding = function () {} | ||
CipherBase.prototype.getAuthTag = function () { | ||
@@ -64,11 +70,11 @@ throw new Error('trying to get auth tag in unsupported state') | ||
try { | ||
this.push(this._final()) | ||
this.push(this.__final()) | ||
} catch (e) { | ||
err = e | ||
} finally { | ||
done(err) | ||
} | ||
done(err) | ||
} | ||
CipherBase.prototype._finalOrDigest = function (outputEnc) { | ||
var outData = this._final() || new Buffer('') | ||
var outData = this.__final() || Buffer.alloc(0) | ||
if (outputEnc) { | ||
@@ -85,5 +91,5 @@ outData = this._toString(outData, outputEnc, true) | ||
} | ||
if (this._encoding !== enc) { | ||
throw new Error('can\'t switch encodings') | ||
} | ||
if (this._encoding !== enc) throw new Error('can\'t switch encodings') | ||
var out = this._decoder.write(value) | ||
@@ -93,3 +99,6 @@ if (fin) { | ||
} | ||
return out | ||
} | ||
module.exports = CipherBase |
{ | ||
"name": "cipher-base", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "abstract base class for crypto-streams", | ||
@@ -24,5 +24,7 @@ "main": "index.js", | ||
"dependencies": { | ||
"inherits": "^2.0.1" | ||
"inherits": "^2.0.1", | ||
"safe-buffer": "^5.0.1" | ||
}, | ||
"devDependencies": { | ||
"standard": "^10.0.2", | ||
"tap-spec": "^4.1.0", | ||
@@ -29,0 +31,0 @@ "tape": "^4.2.0" |
17
test.js
@@ -0,10 +1,12 @@ | ||
var Buffer = require('safe-buffer').Buffer | ||
var CipherBase = require('./') | ||
var test = require('tape') | ||
var CipherBase = require('./') | ||
var inherits = require('inherits') | ||
test('basic version', function (t) { | ||
inherits(Cipher, CipherBase) | ||
function Cipher () { | ||
CipherBase.call(this) | ||
} | ||
inherits(Cipher, CipherBase) | ||
Cipher.prototype._update = function (input) { | ||
@@ -20,3 +22,3 @@ t.ok(Buffer.isBuffer(input)) | ||
var update = cipher.update(utf8, 'utf8', 'base64') + cipher.final('base64') | ||
var string = (new Buffer(update, 'base64')).toString() | ||
var string = (Buffer.from(update, 'base64')).toString() | ||
t.equals(utf8, string) | ||
@@ -26,3 +28,2 @@ t.end() | ||
test('hash mode', function (t) { | ||
inherits(Cipher, CipherBase) | ||
function Cipher () { | ||
@@ -32,2 +33,3 @@ CipherBase.call(this, 'finalName') | ||
} | ||
inherits(Cipher, CipherBase) | ||
Cipher.prototype._update = function (input) { | ||
@@ -43,3 +45,3 @@ t.ok(Buffer.isBuffer(input)) | ||
var update = cipher.update(utf8, 'utf8').finalName('base64') | ||
var string = (new Buffer(update, 'base64')).toString() | ||
var string = (Buffer.from(update, 'base64')).toString() | ||
@@ -50,3 +52,2 @@ t.equals(utf8, string) | ||
test('hash mode as stream', function (t) { | ||
inherits(Cipher, CipherBase) | ||
function Cipher () { | ||
@@ -56,2 +57,3 @@ CipherBase.call(this, 'finalName') | ||
} | ||
inherits(Cipher, CipherBase) | ||
Cipher.prototype._update = function (input) { | ||
@@ -71,3 +73,3 @@ t.ok(Buffer.isBuffer(input)) | ||
var update = cipher.read().toString('base64') | ||
var string = (new Buffer(update, 'base64')).toString() | ||
var string = (Buffer.from(update, 'base64')).toString() | ||
@@ -77,2 +79,3 @@ t.equals(utf8, string) | ||
}) | ||
test('encodings', function (t) { | ||
@@ -79,0 +82,0 @@ inherits(Cipher, CipherBase) |
7952
8
187
2
3
+ Addedsafe-buffer@^5.0.1
+ Addedsafe-buffer@5.2.1(transitive)