aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/envshare/pkg/encoding.test.ts
blob: be0a7f898a0e9c97aaa4b4ad1aed56d36e7de283 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { describe, it, expect, beforeAll } from "@jest/globals";
import { decodeCompositeKey, encodeCompositeKey } from "./encoding";
import { generateKey } from "./encryption";
import { generateId } from "./id";
import crypto from "node:crypto";

beforeAll(() => {
  global.crypto = crypto.webcrypto;
});
describe("composite key encoding", () => {
  it("encodes and decodes composite keys", async () => {
    for (let i = 0; i < 10000; i++) {
      const id = generateId();
      const key = new Uint8Array(await crypto.subtle.exportKey("raw", await generateKey()));

      const encoded = encodeCompositeKey(1, id, key);

      const decoded = decodeCompositeKey(encoded);
      expect(decoded.id).toEqual(id);
      expect(decoded.encryptionKey).toEqual(key);
    }
  });
});