crypto-browserify
crypto-browserify copied to clipboard
Add support for SHA3
SHA3 is becoming more popular and supported by NodeJS, support for it should be added here as well. The popular package "jsSHA" supports this in a relatively easy to wrap way:
class browserSHA3_256 {
#obj;
constructor(hmacKey = undefined) {
if (hmacKey === undefined) {
this.#obj = new jssha("SHA3-256", "ARRAYBUFFER");
} else {
if (!(hmacKey instanceof Uint8Array)) {
throw(new Error('only support Uint8Array for HMAC Key'));
}
this.#obj = new jssha("SHA3-256", "ARRAYBUFFER", {
hmacKey: {
format: "UINT8ARRAY",
value: hmacKey
}
});
}
}
update(data, options = undefined) {
if (options !== undefined) {
throw(new Error('options for "update" not supported'));
}
if (!(data instanceof ArrayBuffer)) {
data = bufferToArrayBuffer(Buffer.from(data));
}
this.#obj.update(data);
return(this);
}
digest(encoding = undefined) {
let retval;
switch (encoding) {
case 'hex':
retval = this.#obj.getHash("HEX");
break;
case undefined:
retval = Buffer.from(this.#obj.getHash("ARRAYBUFFER"));
break;
default:
throw(new Error(`unsupported encoding "${encoding}"`));
}
return(retval);
}
}