Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 78 additions & 114 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,35 +116,22 @@ function Blind(options) {

Blind.prototype.random = function (length) {
var self = this;

var check = this._check('random');
if (check) {
return Promise.reject(check);
}
return new Promise(function (resolve, reject) {

// check properties and arguments

if (!self.skipChecks) {
if (validBinaryEncodings.indexOf(self.binaryEncoding) < 0) {
reject(new RangeError(binaryEncodingMessage));
return;
}

if (!is.number(self.maxRandomLength)) {
reject(new TypeError(maxRandomLengthMessage));
return;
}

if (!is.int(self.maxRandomLength) || self.maxRandomLength < minRandomLength + 1) {
reject(new RangeError(maxRandomLengthRangeMessage));
return;
}

if (!is.number(length)) {
reject(new TypeError('Argument \'length\' must be a number'));
return;
return reject(new TypeError('Argument \'length\' must be a number'));
}

if (!is.int(length) || !is.within(length, minRandomLength, self.maxRandomLength)) {
reject(new RangeError('Argument \'length\' must be an integer between ' + minRandomLength + ' and ' + self.maxRandomLength));
return;
return reject(new RangeError('Argument \'length\' must be an integer between ' + minRandomLength + ' and ' + self.maxRandomLength));
}
}

Expand All @@ -163,45 +150,26 @@ Blind.prototype.random = function (length) {

Blind.prototype.encrypt = function(data, key) {
var self = this;

var check = this._check('encrypt');
if (check) {
return Promise.reject(check);
}
return new Promise(function (resolve, reject) {

// check properties and arguments

if (!self.skipChecks) {
if (validBinaryEncodings.indexOf(self.binaryEncoding) < 0) {
reject(new RangeError(binaryEncodingMessage));
return;
}

if (validEncryptAlgorithms.indexOf(self.encryptAlgorithm) < 0) {
reject(new RangeError(encryptAlgorithmMessage));
return;
}

if (!is.number(self.maxDataLength)) {
reject(new TypeError(maxDataLengthMessage));
return;
}

if (!is.int(self.maxDataLength) || self.maxDataLength < 1) {
reject(new RangeError(maxDataLengthRangeMessage));
return;
}

if (!is.string(data) || !is.within(data.length, 1, self.maxDataLength)) {
reject(new TypeError('Argument \'data\' must be a string between 1 and ' + self.maxDataLength + ' characters long'));
return;
return reject(new TypeError('Argument \'data\' must be a string between 1 and ' + self.maxDataLength + ' characters long'));
}

if (!is.string(key) || !key.length) {
reject(new TypeError('Argument \'key\' must be a string with one or more characters'));
return;
return reject(new TypeError('Argument \'key\' must be a string with one or more characters'));
}

if (!is[self.binaryEncoding](key)) {
reject(new TypeError('Argument \'key\' must be a ' + self.binaryEncoding + ' encoded binary value'));
return;
return reject(new TypeError('Argument \'key\' must be a ' + self.binaryEncoding + ' encoded binary value'));
}
}

Expand All @@ -227,55 +195,35 @@ Blind.prototype.encrypt = function(data, key) {

Blind.prototype.decrypt = function(data, key) {
var self = this;

var check = this._check('decrypt');
if (check) {
return Promise.reject(check);
}
return new Promise(function (resolve, reject) {

// check properties and arguments

if (!self.skipChecks) {
if (validBinaryEncodings.indexOf(self.binaryEncoding) < 0) {
reject(new RangeError(binaryEncodingMessage));
return;
}

if (validEncryptAlgorithms.indexOf(self.encryptAlgorithm) < 0) {
reject(new RangeError(encryptAlgorithmMessage));
return;
}

if (!is.number(self.maxDataLength)) {
reject(new TypeError(maxDataLengthMessage));
return;
}

if (!is.int(self.maxDataLength) || self.maxDataLength < 1) {
reject(new RangeError(maxDataLengthRangeMessage));
return;
}

var minLength = self.binaryEncoding === 'base64' ? 4 : 2;
var encodingMultiplier = self.binaryEncoding === 'base64' ? 4 / 3 : 2;
var maxBlockLength = Math.ceil(self.maxDataLength / maxEncryptBlockSize);
var maxLength = Math.ceil(maxBlockLength * maxEncryptBlockSize * encodingMultiplier);

if (!is.string(data) || !is.within(data.length, minLength, maxLength)) {
reject(new TypeError('Argument \'data\' must be a string between ' + minLength + ' and ' + maxLength + ' characters long'));
return;
return reject(new TypeError('Argument \'data\' must be a string between ' + minLength + ' and ' + maxLength + ' characters long'));
}

if (!is[self.binaryEncoding](data)) {
reject(new TypeError('Argument \'data\' must be a ' + self.binaryEncoding + ' encoded binary value'));
return;
return reject(new TypeError('Argument \'data\' must be a ' + self.binaryEncoding + ' encoded binary value'));
}

if (!is.string(key) || !key.length) {
reject(new TypeError('Argument \'key\' must be a string with one or more characters'));
return;
return reject(new TypeError('Argument \'key\' must be a string with one or more characters'));
}

if (!is[self.binaryEncoding](key)) {
reject(new TypeError('Argument \'key\' must be a ' + self.binaryEncoding + ' encoded binary value'));
return;
return reject(new TypeError('Argument \'key\' must be a ' + self.binaryEncoding + ' encoded binary value'));
}
}

Expand All @@ -302,61 +250,27 @@ Blind.prototype.decrypt = function(data, key) {

Blind.prototype.hash = function(data, salt) {
var self = this;

var check = this._check('hash');
if (check) {
return Promise.reject(check);
}
return new Promise(function (resolve, reject) {

// check properties and arguments

if (!self.skipChecks) {
if (validBinaryEncodings.indexOf(self.binaryEncoding) < 0) {
reject(new RangeError(binaryEncodingMessage));
return;
}

if (!is.number(self.hashLength)) {
reject(new TypeError(hashLengthMessage));
return;
}

if (!is.int(self.hashLength) || self.hashLength < 1) {
reject(new RangeError(hashLengthRangeMessage));
return;
}

if (!is.number(self.hashRounds)) {
reject(new TypeError(hashRoundsMessage));
return;
}

if (!is.int(self.hashRounds) || self.hashRounds < 1) {
reject(new RangeError(hashRoundsRangeMessage));
return;
}

if (!is.number(self.maxDataLength)) {
reject(new TypeError(maxDataLengthMessage));
return;
}

if (!is.int(self.maxDataLength) || self.maxDataLength < 1) {
reject(new RangeError(maxDataLengthRangeMessage));
return;
}

if (!is.string(data) || !is.within(data.length, 1, self.maxDataLength)) {
reject(new TypeError('Argument \'data\' must be a string between 1 and ' + self.maxDataLength + ' characters long'));
return;
return reject(new TypeError('Argument \'data\' must be a string between 1 and ' + self.maxDataLength + ' characters long'));
}

if (salt) {
if (!is.string(salt) || !salt.length) {
reject(new TypeError('Argument \'salt\' must be a string with one or more characters'));
return;
return reject(new TypeError('Argument \'salt\' must be a string with one or more characters'));
}

if (!is[self.binaryEncoding](salt)) {
reject(new TypeError('Argument \'salt\' must a ' + self.binaryEncoding + ' encoded binary value'));
return;
return reject(new TypeError('Argument \'salt\' must a ' + self.binaryEncoding + ' encoded binary value'));
}
}
}
Expand All @@ -373,7 +287,57 @@ Blind.prototype.hash = function(data, salt) {
});
});
};
Blind.prototype._check = function (method) {
if (this.skipChecks) {
return;
}

if (validBinaryEncodings.indexOf(this.binaryEncoding) < 0) {
return new RangeError(binaryEncodingMessage);
}

if (!is.number(this.maxDataLength)) {
return new TypeError(maxDataLengthMessage);
}

if (!is.int(this.maxDataLength) || this.maxDataLength < 1) {
return new RangeError(maxDataLengthRangeMessage);
}

if (method === 'encrypt' || method === 'decrypt') {
if (validEncryptAlgorithms.indexOf(this.encryptAlgorithm) < 0) {
return new RangeError(encryptAlgorithmMessage);
}
}

if (method === 'hash') {
if (!is.number(this.hashLength)) {
return new TypeError(hashLengthMessage);
}

if (!is.int(this.hashLength) || this.hashLength < 1) {
return new RangeError(hashLengthRangeMessage);
}

if (!is.number(this.hashRounds)) {
return new TypeError(hashRoundsMessage);
}

if (!is.int(this.hashRounds) || this.hashRounds < 1) {
return new RangeError(hashRoundsRangeMessage);
}
}

if (method === 'random') {
if (!is.number(this.maxRandomLength)) {
return new TypeError(maxRandomLengthMessage);
}

if (!is.int(this.maxRandomLength) || this.maxRandomLength < minRandomLength + 1) {
return new RangeError(maxRandomLengthRangeMessage);
}
}
};
// static methods

Blind.create = function (options) {
Expand Down