Skip to content

Commit

Permalink
Add argument constraints on x11.digest function
Browse files Browse the repository at this point in the history
 - Argument constraints: exceptions and tests
 - Argument names (consistency / semantics)
 - Rebuilt dist bundles
  • Loading branch information
andyfreer committed May 22, 2018
1 parent 739bbd1 commit 01ad8e3
Show file tree
Hide file tree
Showing 4 changed files with 392 additions and 107 deletions.
146 changes: 102 additions & 44 deletions dist/x11-hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -6283,31 +6283,112 @@ var groestl = require('./lib/groestl');
var bmw = require('./lib/bmw');
var h = require('./lib/helper');

module.exports.blake = function(str,format, output) {
return blake(str,format,output);
/**
* Error codes
*/
var errors = module.exports.errors = {
input_not_specified: 'input not specified',
input_single_invalid_type: 'input must be string when inputFormat is not specified',
input_format_mismatch_string: 'input format mismatch: input should be an string',
input_format_mismatch_array: 'input format mismatch: input should be an array',
input_format_invalid: 'invalid input format',
output_format_invalid: 'invalid output format'
};

/**
* Obtain an x11 hash
* @param input {string|array} input data to hash
* @param inputFormat {number} optional - format of the input: 0: string, 1: 8 bit array, 2: 32 bit array
* @param outputFormat {number} optional - format of the output: 0: string, 1: 8 bit array, 2: 32 bit array
* @returns {string|array} x11 hash of input as a string, 8-bit array or 32-bit array
*/
module.exports.digest = function (input, inputFormat, outputFormat) {

// argument exceptions
if (input === undefined) {
throw (errors.input_not_specified);
} else if (inputFormat === undefined) {

// single input arg must be string
if (!(typeof input === 'string' || input instanceof String)) {
throw (errors.input_single_invalid_type);
}
} else {

// validate input arguments
if (inputFormat === 0) {
if (!(typeof input === 'string' || input instanceof String)) {
throw (errors.input_format_mismatch_string);
}
} else if (inputFormat === 1 || inputFormat === 2) {
if (!Array.isArray(input)) {
throw (errors.input_format_mismatch_array);
}
} else {
throw (errors.input_format_invalid);
}

// validate output format
if (outputFormat !== undefined
&& outputFormat !== 0
&& outputFormat !== 1
&& outputFormat !== 2) {
throw (errors.output_format_invalid);
}
}

// obtain the x11 hash of the input
var a = blake(input, inputFormat, 2);
a = bmw(a, 2, 2);
a = groestl(a, 2, 2);
a = skein(a, 2, 2);
a = jh(a, 2, 2);
a = this.keccak(a, 2, 1);
a = luffa(a, 1, 2);
a = cubehash(a, 2, 2);
a = shavite(a, 2, 2);
a = simd(a, 2, 2);
a = echo(a, 2, 2);
a = a.slice(0, 8);

// output 32-bit array
if (outputFormat === 2) {
return a;
}
// output 8-bit array
else if (outputFormat === 1) {
return h.int32Buffer2Bytes(a);
}
// output string
return h.int32ArrayToHexString(a);
};

// individual x11 hash functions...
module.exports.blake = function (str, format, output) {
return blake(str, format, output);
};

module.exports.bmw = function(str,format, output) {
return bmw(str,format,output);
module.exports.bmw = function (str, format, output) {
return bmw(str, format, output);
};

module.exports.cubehash = function(str,format, output) {
return cubehash(str,format,output);
module.exports.cubehash = function (str, format, output) {
return cubehash(str, format, output);
};

module.exports.echo = function(str,format, output) {
return echo(str,format,output);
module.exports.echo = function (str, format, output) {
return echo(str, format, output);
};

module.exports.groestl = function(str,format, output) {
return groestl(str,format,output);
module.exports.groestl = function (str, format, output) {
return groestl(str, format, output);
};

module.exports.jh = function(str,format, output) {
return jh(str,format,output);
module.exports.jh = function (str, format, output) {
return jh(str, format, output);
};

module.exports.keccak = function(str,format, output) {
module.exports.keccak = function (str, format, output) {
var msg = str;
if (format === 2) {
msg = h.int32Buffer2Bytes(str);
Expand All @@ -6321,43 +6402,20 @@ module.exports.keccak = function(str,format, output) {

};

module.exports.luffa = function(str,format, output) {
return luffa(str,format,output);
};

module.exports.shavite = function(str,format, output) {
return shavite(str,format,output);
module.exports.luffa = function (str, format, output) {
return luffa(str, format, output);
};

module.exports.simd = function(str,format, output) {
return simd(str,format,output);
module.exports.shavite = function (str, format, output) {
return shavite(str, format, output);
};

module.exports.skein = function(str,format, output) {
return skein(str,format,output);
module.exports.simd = function (str, format, output) {
return simd(str, format, output);
};


module.exports.digest = function(str,format, output) {
var a = blake(str,format,2);
a = bmw(a,2,2);
a = groestl(a,2,2);
a = skein(a,2,2);
a = jh(a,2,2);
a = this.keccak(a,2,1);
a = luffa(a,1,2);
a = cubehash(a,2,2);
a = shavite(a,2,2);
a = simd(a,2,2);
a = echo(a,2,2);
a = a.slice(0,8);
if (output === 2) {
return a;
}
else if (output === 1) {
return h.int32Buffer2Bytes(a);
}
return h.int32ArrayToHexString(a);
module.exports.skein = function (str, format, output) {
return skein(str, format, output);
};

},{"./lib/blake":2,"./lib/bmw":3,"./lib/cubehash":4,"./lib/echo":5,"./lib/groestl":6,"./lib/helper":7,"./lib/jh":8,"./lib/keccak":9,"./lib/luffa":10,"./lib/shavite":12,"./lib/simd":13,"./lib/skein":14}]},{},[]);
2 changes: 1 addition & 1 deletion dist/x11-hash.min.js

Large diffs are not rendered by default.

146 changes: 102 additions & 44 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,112 @@ var groestl = require('./lib/groestl');
var bmw = require('./lib/bmw');
var h = require('./lib/helper');

module.exports.blake = function(str,format, output) {
return blake(str,format,output);
/**
* Error codes
*/
var errors = module.exports.errors = {
input_not_specified: 'input not specified',
input_single_invalid_type: 'input must be string when inputFormat is not specified',
input_format_mismatch_string: 'input format mismatch: input should be an string',
input_format_mismatch_array: 'input format mismatch: input should be an array',
input_format_invalid: 'invalid input format',
output_format_invalid: 'invalid output format'
};

module.exports.bmw = function(str,format, output) {
return bmw(str,format,output);
/**
* Obtain an x11 hash
* @param input {string|array} input data to hash
* @param inputFormat {number} optional - format of the input: 0: string, 1: 8 bit array, 2: 32 bit array
* @param outputFormat {number} optional - format of the output: 0: string, 1: 8 bit array, 2: 32 bit array
* @returns {string|array} x11 hash of input as a string, 8-bit array or 32-bit array
*/
module.exports.digest = function (input, inputFormat, outputFormat) {

// argument exceptions
if (input === undefined) {
throw (errors.input_not_specified);
} else if (inputFormat === undefined) {

// single input arg must be string
if (!(typeof input === 'string' || input instanceof String)) {
throw (errors.input_single_invalid_type);
}
} else {

// validate input arguments
if (inputFormat === 0) {
if (!(typeof input === 'string' || input instanceof String)) {
throw (errors.input_format_mismatch_string);
}
} else if (inputFormat === 1 || inputFormat === 2) {
if (!Array.isArray(input)) {
throw (errors.input_format_mismatch_array);
}
} else {
throw (errors.input_format_invalid);
}

// validate output format
if (outputFormat !== undefined
&& outputFormat !== 0
&& outputFormat !== 1
&& outputFormat !== 2) {
throw (errors.output_format_invalid);
}
}

// obtain the x11 hash of the input
var a = blake(input, inputFormat, 2);
a = bmw(a, 2, 2);
a = groestl(a, 2, 2);
a = skein(a, 2, 2);
a = jh(a, 2, 2);
a = this.keccak(a, 2, 1);
a = luffa(a, 1, 2);
a = cubehash(a, 2, 2);
a = shavite(a, 2, 2);
a = simd(a, 2, 2);
a = echo(a, 2, 2);
a = a.slice(0, 8);

// output 32-bit array
if (outputFormat === 2) {
return a;
}
// output 8-bit array
else if (outputFormat === 1) {
return h.int32Buffer2Bytes(a);
}
// output string
return h.int32ArrayToHexString(a);
};

module.exports.cubehash = function(str,format, output) {
return cubehash(str,format,output);
// individual x11 hash functions...
module.exports.blake = function (str, format, output) {
return blake(str, format, output);
};

module.exports.echo = function(str,format, output) {
return echo(str,format,output);
module.exports.bmw = function (str, format, output) {
return bmw(str, format, output);
};

module.exports.groestl = function(str,format, output) {
return groestl(str,format,output);
module.exports.cubehash = function (str, format, output) {
return cubehash(str, format, output);
};

module.exports.jh = function(str,format, output) {
return jh(str,format,output);
module.exports.echo = function (str, format, output) {
return echo(str, format, output);
};

module.exports.keccak = function(str,format, output) {
module.exports.groestl = function (str, format, output) {
return groestl(str, format, output);
};

module.exports.jh = function (str, format, output) {
return jh(str, format, output);
};

module.exports.keccak = function (str, format, output) {
var msg = str;
if (format === 2) {
msg = h.int32Buffer2Bytes(str);
Expand All @@ -51,41 +132,18 @@ module.exports.keccak = function(str,format, output) {

};

module.exports.luffa = function(str,format, output) {
return luffa(str,format,output);
};

module.exports.shavite = function(str,format, output) {
return shavite(str,format,output);
module.exports.luffa = function (str, format, output) {
return luffa(str, format, output);
};

module.exports.simd = function(str,format, output) {
return simd(str,format,output);
module.exports.shavite = function (str, format, output) {
return shavite(str, format, output);
};

module.exports.skein = function(str,format, output) {
return skein(str,format,output);
module.exports.simd = function (str, format, output) {
return simd(str, format, output);
};


module.exports.digest = function(str,format, output) {
var a = blake(str,format,2);
a = bmw(a,2,2);
a = groestl(a,2,2);
a = skein(a,2,2);
a = jh(a,2,2);
a = this.keccak(a,2,1);
a = luffa(a,1,2);
a = cubehash(a,2,2);
a = shavite(a,2,2);
a = simd(a,2,2);
a = echo(a,2,2);
a = a.slice(0,8);
if (output === 2) {
return a;
}
else if (output === 1) {
return h.int32Buffer2Bytes(a);
}
return h.int32ArrayToHexString(a);
module.exports.skein = function (str, format, output) {
return skein(str, format, output);
};
Loading

0 comments on commit 01ad8e3

Please sign in to comment.