This repository has been archived on 2023-04-04. You can view files and clone it, but cannot push or open issues or pull requests.
webcrypto/test/crypto.ts

61 lines
1.8 KiB
TypeScript
Raw Normal View History

2019-01-25 10:43:13 +00:00
import assert from "assert";
2020-03-23 08:04:24 +00:00
import { WebcryptoTest } from "@peculiar/webcrypto-test";
import * as core from "webcrypto-core";
2019-01-25 10:43:13 +00:00
import { Crypto } from "../src";
2020-03-23 08:04:24 +00:00
const crypto = new Crypto();
WebcryptoTest.check(crypto as any, {});
2019-01-25 10:43:13 +00:00
context("Crypto", () => {
context("getRandomValues", () => {
it("Uint8Array", () => {
const array = new Uint8Array(5);
const array2 = crypto.getRandomValues(array);
assert.notStrictEqual(Buffer.from(array).toString("hex"), "0000000000");
assert.strictEqual(Buffer.from(array2).equals(array), true);
2019-01-25 10:43:13 +00:00
});
it("Uint16Array", () => {
const array = new Uint16Array(5);
const array2 = crypto.getRandomValues(array);
assert.notStrictEqual(Buffer.from(array).toString("hex"), "00000000000000000000");
assert.strictEqual(Buffer.from(array2).equals(Buffer.from(array)), true);
2019-01-25 10:43:13 +00:00
});
});
it("Import wrong named curve", async () => {
const spki = Buffer.from("MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAETzlbSDQWz+1nwEHsrT516OAEX5YWzwVYj39BH+Rv5yoP9yLgM5wIXgOls5DoLDJVQ+45XDrD/xjSCcul5NACZw==", "base64");
await assert.rejects(crypto.subtle.importKey(
"spki",
spki,
{ name: "ECDSA", namedCurve: "K-256" } as Algorithm,
false,
["verify"]), core.CryptoError);
});
2021-02-01 14:17:31 +00:00
it("HKDF derive HMAC key", async () => {
const hkdf = await crypto.subtle.importKey("raw", new Uint8Array([1, 2, 3, 4, 5]), { name: "HKDF" }, false, ["deriveKey"]);
const hmac = await crypto.subtle.deriveKey({
name: "HKDF",
hash: "SHA-256",
info: new Uint8Array([1, 2, 3, 4, 5]),
salt: new Uint8Array([1, 2, 3, 4, 5]),
} as globalThis.HkdfParams,
hkdf,
{
name: "HMAC",
hash: "SHA-1",
} as globalThis.HmacImportParams,
false,
["sign"]);
assert.strictEqual((hmac.algorithm as globalThis.HmacKeyAlgorithm).length, 512);
});
2019-01-25 10:43:13 +00:00
});