Skip to content

Add support for SHA3Β #211

@rkeene

Description

@rkeene

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);
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions