types(createHash): add no implicit any corrections to createHash

This commit is contained in:
Sean Larkin 2018-04-27 13:33:30 -05:00
parent 56170c3d46
commit 4889d222b0
1 changed files with 53 additions and 9 deletions

View File

@ -4,14 +4,31 @@
*/
"use strict";
/** @typedef {import("crypto").Hash} Hash */
/** @typedef {import("crypto").Utf8AsciiLatin1Encoding} Utf8AsciiLatin1Encoding */
/** @typedef {import("crypto").HexBase64Latin1Encoding} HexBase64Latin1Encoding*/
/** @typedef {new(...args:any[]) => Hash & Hash} HashFunctionConstructor */
const BULK_SIZE = 1000;
class BulkUpdateDecorator {
/**
* Creates an instance of BulkUpdateDecorator.
* @param {Hash} hash the hash algorithm instance
* @memberof BulkUpdateDecorator
*/
constructor(hash) {
this.hash = hash;
this.buffer = "";
}
/**
* @description
* @param {string | Buffer | DataView} data input data causing hash update
* @param {Utf8AsciiLatin1Encoding} inputEncoding encoding used for input
* @returns {BulkUpdateDecorator} returns the original BulkUpdateDecorator instance
* @memberof BulkUpdateDecorator
*/
update(data, inputEncoding) {
if (
inputEncoding !== undefined ||
@ -33,14 +50,21 @@ class BulkUpdateDecorator {
return this;
}
/**
* @description generates a digest string of the current hash
* @param {HexBase64Latin1Encoding} encoding the digest encoding format
* @returns {string} returns the string value of the digest
* @memberof BulkUpdateDecorator
*/
digest(encoding) {
if (this.buffer.length > 0) {
this.hash.update(this.buffer);
}
var digestResult = this.hash.digest(encoding);
return typeof digestResult === "string"
? digestResult
: digestResult.toString();
// var digestResult = this.hash.digest(encoding);
// return typeof digestResult === "string"
// ? digestResult
// : digestResult.toString();
return this.hash.digest(encoding);
}
}
@ -50,20 +74,38 @@ class DebugHash {
this.string = "";
}
update(data, inputEncoding) {
if (typeof data !== "string") data = data.toString("utf-8");
/**
* @description this triggers a hash update based on new input data
* @param {string | Buffer | DataView} data new input data for updated hash
* @returns {DebugHash} retuns self instance of DebugHash
* @memberof DebugHash
*/
update(data) {
if (typeof data !== "string") {
data = data.toString();
}
this.string += data;
return this;
}
digest(encoding) {
/**
* @description converts hash to digested string format
* @returns {string} returns a digested hash to string
* @memberof DebugHash
*/
digest() {
return this.string.replace(/[^a-z0-9]+/gi, m =>
Buffer.from(m).toString("hex")
);
}
}
module.exports = algorithm => {
/**
* @description create a hash which can be applied in bulk using specified hashing algo
* @param {string|HashFunctionConstructor} algorithm either a cyrpto compatible cipher string or separate hashing function
* @return {DebugHash|BulkUpdateDecorator} creates the instance that manages the hash and hash changes
*/
function createHash(algorithm) {
if (typeof algorithm === "function") {
return new BulkUpdateDecorator(new algorithm());
}
@ -74,4 +116,6 @@ module.exports = algorithm => {
default:
return new BulkUpdateDecorator(require("crypto").createHash(algorithm));
}
};
}
module.exports = createHash;