[Pkg-javascript-commits] [node-browserify-aes] 16/43: encrypter/decrypter: formatting, an no instanceof required
Bastien Roucariès
rouca at moszumanska.debian.org
Thu Sep 7 14:41:58 UTC 2017
This is an automated email from the git hooks/post-receive script.
rouca pushed a commit to branch master
in repository node-browserify-aes.
commit 7aae55a8305d0a4729f58080d6ae7d306c2f4142
Author: Daniel Cousens <github at dcousens.com>
Date: Fri May 19 14:38:04 2017 +1000
encrypter/decrypter: formatting, an no instanceof required
---
decrypter.js | 44 +++++++++++++++++++-------------------------
encrypter.js | 31 ++++++++++++-------------------
2 files changed, 31 insertions(+), 44 deletions(-)
diff --git a/decrypter.js b/decrypter.js
index 37ae849..97b1ceb 100644
--- a/decrypter.js
+++ b/decrypter.js
@@ -6,12 +6,9 @@ var StreamCipher = require('./streamCipher')
var AuthCipher = require('./authCipher')
var ebtk = require('evp_bytestokey')
-inherits(Decipher, Transform)
function Decipher (mode, key, iv) {
- if (!(this instanceof Decipher)) {
- return new Decipher(mode, key, iv)
- }
Transform.call(this)
+
this._cache = new Splitter()
this._last = void 0
this._cipher = new aes.AES(key)
@@ -19,6 +16,9 @@ function Decipher (mode, key, iv) {
this._mode = mode
this._autopadding = true
}
+
+inherits(Decipher, Transform)
+
Decipher.prototype._update = function (data) {
this._cache.add(data)
var chunk
@@ -30,6 +30,7 @@ Decipher.prototype._update = function (data) {
}
return Buffer.concat(out)
}
+
Decipher.prototype._final = function () {
var chunk = this._cache.flush()
if (this._autopadding) {
@@ -38,6 +39,7 @@ Decipher.prototype._final = function () {
throw new Error('data not multiple of block length')
}
}
+
Decipher.prototype.setAutoPadding = function (setTo) {
this._autopadding = !!setTo
return this
@@ -71,9 +73,7 @@ Splitter.prototype.get = function (autoPadding) {
}
Splitter.prototype.flush = function () {
- if (this.cache.length) {
- return this.cache
- }
+ if (this.cache.length) return this.cache
}
function unpad (last) {
@@ -102,36 +102,30 @@ var modelist = {
function createDecipheriv (suite, password, iv) {
var config = modes[suite.toLowerCase()]
- if (!config) {
- throw new TypeError('invalid suite type')
- }
- if (typeof iv === 'string') {
- iv = Buffer.from(iv)
- }
- if (typeof password === 'string') {
- password = Buffer.from(password)
- }
- if (password.length !== config.key / 8) {
- throw new TypeError('invalid key length ' + password.length)
- }
- if (iv.length !== config.iv) {
- throw new TypeError('invalid iv length ' + iv.length)
- }
+ if (!config) throw new TypeError('invalid suite type')
+
+ if (typeof iv === 'string') iv = Buffer.from(iv)
+ if (iv.length !== config.iv) throw new TypeError('invalid iv length ' + iv.length)
+
+ if (typeof password === 'string') password = Buffer.from(password)
+ if (password.length !== config.key / 8) throw new TypeError('invalid key length ' + password.length)
+
if (config.type === 'stream') {
return new StreamCipher(modelist[config.mode], password, iv, true)
} else if (config.type === 'auth') {
return new AuthCipher(modelist[config.mode], password, iv, true)
}
+
return new Decipher(modelist[config.mode], password, iv)
}
function createDecipher (suite, password) {
var config = modes[suite.toLowerCase()]
- if (!config) {
- throw new TypeError('invalid suite type')
- }
+ if (!config) throw new TypeError('invalid suite type')
+
var keys = ebtk(password, false, config.key, config.iv)
return createDecipheriv(suite, keys.key, keys.iv)
}
+
exports.createDecipher = createDecipher
exports.createDecipheriv = createDecipheriv
diff --git a/encrypter.js b/encrypter.js
index 0a92179..b4363da 100644
--- a/encrypter.js
+++ b/encrypter.js
@@ -8,7 +8,6 @@ var inherits = require('inherits')
var modes = require('./modes.json')
function Cipher (mode, key, iv) {
- if (!(this instanceof Cipher)) return new Cipher(mode, key, iv)
Transform.call(this)
this._cache = new Splitter()
@@ -17,6 +16,7 @@ function Cipher (mode, key, iv) {
this._mode = mode
this._autopadding = true
}
+
inherits(Cipher, Transform)
Cipher.prototype._update = function (data) {
@@ -92,34 +92,27 @@ var modelist = {
function createCipheriv (suite, password, iv) {
var config = modes[suite.toLowerCase()]
- if (!config) {
- throw new TypeError('invalid suite type')
- }
- if (typeof iv === 'string') {
- iv = Buffer.from(iv)
- }
- if (typeof password === 'string') {
- password = Buffer.from(password)
- }
- if (password.length !== config.key / 8) {
- throw new TypeError('invalid key length ' + password.length)
- }
- if (iv.length !== config.iv) {
- throw new TypeError('invalid iv length ' + iv.length)
- }
+ if (!config) throw new TypeError('invalid suite type')
+
+ if (typeof password === 'string') password = Buffer.from(password)
+ if (password.length !== config.key / 8) throw new TypeError('invalid key length ' + password.length)
+
+ if (typeof iv === 'string') iv = Buffer.from(iv)
+ if (iv.length !== config.iv) throw new TypeError('invalid iv length ' + iv.length)
+
if (config.type === 'stream') {
return new StreamCipher(modelist[config.mode], password, iv)
} else if (config.type === 'auth') {
return new AuthCipher(modelist[config.mode], password, iv)
}
+
return new Cipher(modelist[config.mode], password, iv)
}
function createCipher (suite, password) {
var config = modes[suite.toLowerCase()]
- if (!config) {
- throw new TypeError('invalid suite type')
- }
+ if (!config) throw new TypeError('invalid suite type')
+
var keys = ebtk(password, false, config.key, config.iv)
return createCipheriv(suite, keys.key, keys.iv)
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-browserify-aes.git
More information about the Pkg-javascript-commits
mailing list