Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
for (const headerName of Object.keys(headers)) {
if (headerName.toLowerCase() === SHA256_HEADER) {
return headers[headerName];
}
}
if (body == undefined) {
return "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
} else if (
typeof body === "string" ||
ArrayBuffer.isView(body) ||
isArrayBuffer(body)
) {
const hashCtor = new hashConstructor();
hashCtor.update(body);
return toHex(await hashCtor.digest());
}
// As any defined body that is not a string or binary data is a stream, this
// body is unsignable. Attempt to send the request with an unsigned payload,
// which may or may not be accepted by the service.
return UNSIGNED_PAYLOAD;
}
it(`should calculate a MD5 hash of ${expected} for test vector ${++idx}`, async () => {
const hash = new Md5();
hash.update(fromBase64(input));
expect(toHex(await hash.digest())).toBe(expected);
});
}
it("calculates the SHA256 hash of a stream", async () => {
const result = await calculateSha256(
Sha256,
createReadStream(temporaryFile)
);
expect(result instanceof Uint8Array).toBe(true);
expect(toHex(result)).toBe(
"24dabf4db3774a3224d571d4c089a9c570c3045dbe1e67ee9ee2e2677f57dbe0"
);
});
it("computes tree hashes of string bodies", async () => {
const treeHash = new TreeHash(Sha256, fromUtf8);
treeHash.update("bar");
const results = await treeHash.digest();
expect(toHex(results)).toEqual(
"fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9"
);
});
it("calculates the SHA256 hash of a blob", async () => {
const result = await calculateSha256(Sha256, blob);
expect(result instanceof Uint8Array).toBe(true);
expect(toHex(result)).toBe(
"24dabf4db3774a3224d571d4c089a9c570c3045dbe1e67ee9ee2e2677f57dbe0"
);
});
});
],
[
fromHex('616161616161616161616161616161616161616161616161616161616161616161616161616161'),
fromHex('b4d5e56e929ba4cda349e9274e3603d0be246b82016bca20f363963c5f2d6845'),
],
[
fromHex('61616161616161616161616161616161616161616161616161616161616161616161616161616161'),
fromHex('e33cdf9c7f7120b98e8c78408953e07f2ecd183006b5606df349b4c212acf43e'),
],
[
fromHex('6161616161616161616161616161616161616161616161616161616161616161616161616161616161'),
fromHex('c0f8bd4dbc2b0c03107c1c37913f2a7501f521467f45dd0fef6958e9a4692719'),
],
[
fromHex('616161616161616161616161616161616161616161616161616161616161616161616161616161616161'),
fromHex('7a538607fdaab9296995929f451565bbb8142e1844117322aafd2b3d76b01aff'),
],
[
fromHex('61616161616161616161616161616161616161616161616161616161616161616161616161616161616161'),
fromHex('66d34fba71f8f450f7e45598853e53bfc23bbd129027cbb131a2f4ffd7878cd0'),
],
[
fromHex('6161616161616161616161616161616161616161616161616161616161616161616161616161616161616161'),
fromHex('16849877c6c21ef0bfa68e4f6747300ddb171b170b9f00e189edc4c2fc4db93e'),
],
[
fromHex('616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161'),
fromHex('52789e3423b72beeb898456a4f49662e46b0cbb960784c5ef4b1399d327e7c27'),
],
[
fromHex('61616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161'),
fromHex('6643110c5628fff59edf76d82d5bf573bf800f16a4d65dfb1e5d6f1a46296d0b'),
millionChars,
fromHex('cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0'),
],
[
fromHex('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'),
fromHex('45ad4b37c6e2fc0a2cfcc1b5da524132ec707615c2cae1dbbc43c97aa521db81')
]
];
/**
* @see https://tools.ietf.org/html/rfc4231
*/
export const hmacTestVectors: Array<[Uint8Array, Uint8Array, Uint8Array]> = [
[
fromHex('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b'),
fromHex('4869205468657265'),
fromHex('b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7'),
],
[
fromHex('4a656665'),
fromHex('7768617420646f2079612077616e7420666f72206e6f7468696e673f'),
fromHex('5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843'),
],
[
fromHex('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'),
fromHex('dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd'),
fromHex('773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe'),
],
[
fromHex('0102030405060708090a0b0c0d0e0f10111213141516171819'),
fromHex('cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd'),
fromHex('82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b'),
]
];
/**
* @see https://tools.ietf.org/html/rfc4231
*/
export const hmacTestVectors: Array<[Uint8Array, Uint8Array, Uint8Array]> = [
[
fromHex('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b'),
fromHex('4869205468657265'),
fromHex('b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7'),
],
[
fromHex('4a656665'),
fromHex('7768617420646f2079612077616e7420666f72206e6f7468696e673f'),
fromHex('5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843'),
],
[
fromHex('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'),
fromHex('dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd'),
fromHex('773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe'),
],
[
fromHex('0102030405060708090a0b0c0d0e0f10111213141516171819'),
fromHex('cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd'),
fromHex('82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b'),
],
[
fromHex('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'),
fromHex('54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a65204b6579202d2048617368204b6579204669727374'),
fromHex('60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54'),
],
],
[
fromHex('6161616161616161616161616161616161616161616161616161616161616161616161616161'),
fromHex('365a9c3e2c2af0a56e47a9dac51c2c5381bf8f41273bad3175e0e619126ad087'),
],
[
fromHex('616161616161616161616161616161616161616161616161616161616161616161616161616161'),
fromHex('b4d5e56e929ba4cda349e9274e3603d0be246b82016bca20f363963c5f2d6845'),
],
[
fromHex('61616161616161616161616161616161616161616161616161616161616161616161616161616161'),
fromHex('e33cdf9c7f7120b98e8c78408953e07f2ecd183006b5606df349b4c212acf43e'),
],
[
fromHex('6161616161616161616161616161616161616161616161616161616161616161616161616161616161'),
fromHex('c0f8bd4dbc2b0c03107c1c37913f2a7501f521467f45dd0fef6958e9a4692719'),
],
[
fromHex('616161616161616161616161616161616161616161616161616161616161616161616161616161616161'),
fromHex('7a538607fdaab9296995929f451565bbb8142e1844117322aafd2b3d76b01aff'),
],
[
fromHex('61616161616161616161616161616161616161616161616161616161616161616161616161616161616161'),
fromHex('66d34fba71f8f450f7e45598853e53bfc23bbd129027cbb131a2f4ffd7878cd0'),
],
[
fromHex('6161616161616161616161616161616161616161616161616161616161616161616161616161616161616161'),
fromHex('16849877c6c21ef0bfa68e4f6747300ddb171b170b9f00e189edc4c2fc4db93e'),
],
[
fromHex('616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161'),
fromHex('52789e3423b72beeb898456a4f49662e46b0cbb960784c5ef4b1399d327e7c27'),
fromHex('111bb261277afd65f0744b247cd3e47d386d71563d0ed995517807d5ebd4fba3'),
],
[
fromHex('616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161'),
fromHex('11ee391211c6256460b6ed375957fadd8061cafbb31daf967db875aebd5aaad4'),
],
[
fromHex('61616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161'),
fromHex('35d5fc17cfbbadd00f5e710ada39f194c5ad7c766ad67072245f1fad45f0f530'),
],
[
fromHex('6161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161'),
fromHex('f506898cc7c2e092f9eb9fadae7ba50383f5b46a2a4fe5597dbb553a78981268'),
],
[
fromHex('616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161'),
fromHex('7d3e74a05d7db15bce4ad9ec0658ea98e3f06eeecf16b4c6fff2da457ddc2f34'),
],
[
fromHex('61616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161'),
fromHex('ffe054fe7ae0cb6dc65c3af9b61d5209f439851db43d0ba5997337df154668eb'),
],
[
fromHex('de188941a3375d3a8a061e67576e926dc71a7fa3f0cceb97452b4d3227965f9ea8cc75076d9fb9c5417aa5cb30fc22198b34982dbb629e'),
fromHex('038051e9c324393bd1ca1978dd0952c2aa3742ca4f1bd5cd4611cea83892d382'),
],
[
millionChars,
fromHex('cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0'),
],
[
fromHex('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'),