mirror of
https://github.com/denoland/deno.git
synced 2025-09-23 10:52:33 +00:00

Some checks are pending
ci / test release macos-aarch64 (push) Blocked by required conditions
ci / bench release linux-x86_64 (push) Blocked by required conditions
ci / lint debug linux-x86_64 (push) Blocked by required conditions
ci / lint debug macos-x86_64 (push) Blocked by required conditions
ci / lint debug windows-x86_64 (push) Blocked by required conditions
ci / test debug linux-x86_64 (push) Blocked by required conditions
ci / test release linux-x86_64 (push) Blocked by required conditions
ci / test debug macos-x86_64 (push) Blocked by required conditions
ci / test release macos-x86_64 (push) Blocked by required conditions
ci / test debug windows-x86_64 (push) Blocked by required conditions
ci / test release windows-x86_64 (push) Blocked by required conditions
ci / build libs (push) Blocked by required conditions
ci / pre-build (push) Waiting to run
ci / test debug linux-aarch64 (push) Blocked by required conditions
ci / test release linux-aarch64 (push) Blocked by required conditions
ci / test debug macos-aarch64 (push) Blocked by required conditions
ci / publish canary (push) Blocked by required conditions
Fixes #30759
The buffer validation is based on Node.js implementation:
591ba692bf/src/crypto/crypto_util.h (L467-L472)
39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
// Copyright 2018-2025 the Deno authors. MIT license.
|
|
import * as crypto from "node:crypto";
|
|
import { assertEquals, assertThrows } from "@std/assert";
|
|
import { Buffer } from "node:buffer";
|
|
|
|
Deno.test("timingSafeEqual ArrayBuffer and TypedArray", async () => {
|
|
const data = new TextEncoder().encode("foo"); // Uint8Array
|
|
|
|
const hash = await crypto.subtle.digest("SHA-256", data); // ArrayBuffer
|
|
const ui8a = new Uint8Array(hash); // Uint8Array
|
|
|
|
// @ts-ignore crypto.timingSafeEqual accepts ArrayBuffer
|
|
const eq = crypto.timingSafeEqual(hash, ui8a);
|
|
assertEquals(eq, true);
|
|
});
|
|
|
|
Deno.test("timingSafeEqual rejects non ArrayBuffer/TypedArray", () => {
|
|
const str = "foo";
|
|
const buf = Buffer.from(str);
|
|
const i = 123;
|
|
|
|
assertThrows(
|
|
() => {
|
|
// @ts-expect-error testing invalid input
|
|
crypto.timingSafeEqual(str, buf);
|
|
},
|
|
'TypeError: The "buf1" argument must be an instance of Buffer, ArrayBuffer, TypedArray, or DataView.' +
|
|
"Received type string ('foo')",
|
|
);
|
|
|
|
assertThrows(
|
|
() => {
|
|
// @ts-expect-error testing invalid input
|
|
crypto.timingSafeEqual(buf, i);
|
|
},
|
|
'TypeError: The "buf2" argument must be an instance of Buffer, ArrayBuffer, TypedArray, or DataView.' +
|
|
"Received type number (123)",
|
|
);
|
|
});
|