Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for SHA3 #211

Open
rkeene opened this issue Apr 5, 2022 · 0 comments
Open

Add support for SHA3 #211

rkeene opened this issue Apr 5, 2022 · 0 comments

Comments

@rkeene
Copy link

rkeene commented Apr 5, 2022

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);
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant