aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/envshare/pkg/encryption.test.ts
blob: 5d4cf15e98eaddba5e7b4377b6ec9e3590fcaf4c (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
24
import { describe, it, expect, beforeAll } from "@jest/globals";
import { decrypt, encrypt } from "./encryption";
import crypto from "node:crypto";
import { toBase58 } from "../util/base58";

beforeAll(() => {
  global.crypto = crypto.webcrypto;
});
describe("aes", () => {
  it("encrypts and decrypts correctly", async () => {
    for (let i = 0; i < 500; i++) {
      const buf = new Uint8Array(Math.ceil(Math.random() * 10 * i));
      crypto.getRandomValues(buf);

      const text = toBase58(buf);

      const { encrypted, key, iv } = await encrypt(text);

      const decrypted = await decrypt(toBase58(encrypted), key, toBase58(iv), 2);

      expect(decrypted).toEqual(text);
    }
  }, 30_000);
});