Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it(`should calculate a ${supportedHash} hmac of ${expected} for an input of ${data} and a key of ${key}`, async () => {
const hash = new Hash(supportedHash, fromString(key, "hex"));
hash.update(fromString(data, "hex"));
const { buffer } = await hash.digest();
expect(fromArrayBuffer(buffer).toString("hex")).toMatch(expected);
});
}
it(`should calculate a ${supportedHash} hash of ${expected} for an input of ${input}`, async () => {
const hash = new Hash(supportedHash);
hash.update(fromString(input, "base64"));
const { buffer } = await hash.digest();
expect(fromArrayBuffer(buffer).toString("hex")).toBe(expected);
});
}
it(`should calculate a ${supportedHash} hmac of ${expected} for an input of ${data} and a key of ${key}`, async () => {
const hash = new Hash(supportedHash, fromString(key, "hex"));
hash.update(fromString(data, "hex"));
const { buffer } = await hash.digest();
expect(fromArrayBuffer(buffer).toString("hex")).toMatch(expected);
});
}
export function fromUtf8(input: string): Uint8Array {
const buf = fromString(input, "utf8");
return new Uint8Array(
buf.buffer,
buf.byteOffset,
buf.byteLength / Uint8Array.BYTES_PER_ELEMENT
);
}
function castSourceData(toCast: SourceData, encoding?: string): Buffer {
if (Buffer.isBuffer(toCast)) {
return toCast;
}
if (typeof toCast === "string") {
return fromString(toCast, encoding);
}
if (ArrayBuffer.isView(toCast)) {
return fromArrayBuffer(toCast.buffer, toCast.byteOffset, toCast.byteLength);
}
return fromArrayBuffer(toCast);
}
export function fromBase64(input: string): Uint8Array {
const buffer = fromString(input, "base64");
return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
}