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

Incomplete Typescript types #18

Open
Esya opened this issue Mar 25, 2021 · 0 comments
Open

Incomplete Typescript types #18

Esya opened this issue Mar 25, 2021 · 0 comments

Comments

@Esya
Copy link

Esya commented Mar 25, 2021

As seen here, the code already supports using an ArrayBuffer as input for the key when instanciating HmacSha512, but the types state that it only accepts a string as the key (see below) - the types should be updated to reflect that it can accept other types as well.

js-sha512/index.d.ts

Lines 37 to 51 in b6c4431

interface Hmac {
/**
* Computes a Hash-based message authentication code (HMAC) using a secret key
*
* @param secretKey The Secret Key
* @param message The message you want to hash.
*/
(secretKey: string, message: Message): string;
/**
* Create a hash object using a secret key.
*
* @param secretKey The Secret Key
*/
create(secretKey: string): Hasher;

Ref :

js-sha512/src/sha512.js

Lines 819 to 859 in b6c4431

function HmacSha512(key, bits, sharedMemory) {
var notString, type = typeof key;
if (type !== 'string') {
if (type === 'object') {
if (key === null) {
throw new Error(INPUT_ERROR);
} else if (ARRAY_BUFFER && key.constructor === ArrayBuffer) {
key = new Uint8Array(key);
} else if (!Array.isArray(key)) {
if (!ARRAY_BUFFER || !ArrayBuffer.isView(key)) {
throw new Error(INPUT_ERROR);
}
}
} else {
throw new Error(INPUT_ERROR);
}
notString = true;
}
var length = key.length;
if (!notString) {
var bytes = [], length = key.length, index = 0, code;
for (var i = 0; i < length; ++i) {
code = key.charCodeAt(i);
if (code < 0x80) {
bytes[index++] = code;
} else if (code < 0x800) {
bytes[index++] = (0xc0 | (code >> 6));
bytes[index++] = (0x80 | (code & 0x3f));
} else if (code < 0xd800 || code >= 0xe000) {
bytes[index++] = (0xe0 | (code >> 12));
bytes[index++] = (0x80 | ((code >> 6) & 0x3f));
bytes[index++] = (0x80 | (code & 0x3f));
} else {
code = 0x10000 + (((code & 0x3ff) << 10) | (key.charCodeAt(++i) & 0x3ff));
bytes[index++] = (0xf0 | (code >> 18));
bytes[index++] = (0x80 | ((code >> 12) & 0x3f));
bytes[index++] = (0x80 | ((code >> 6) & 0x3f));
bytes[index++] = (0x80 | (code & 0x3f));
}
}
key = bytes;

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