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 accept string data", async () => {
const hash = new Hash("md5");
hash.update("");
const { buffer } = await hash.digest();
expect(fromArrayBuffer(buffer).toString("hex")).toBe(
"d41d8cd98f00b204e9800998ecf8427e"
);
});
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);
});
}
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);
}
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 toUtf8(input: Uint8Array): string {
return fromArrayBuffer(
input.buffer,
input.byteOffset,
input.byteLength
).toString("utf8");
}
export function toBase64(input: Uint8Array): string {
return fromArrayBuffer(
input.buffer,
input.byteOffset,
input.byteLength
).toString("base64");
}
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);
}