Encrypted, ephemeral, private memos on atproto
1import { expect } from "@std/expect";
2import { generateKeys } from "./keys.ts";
3import { encryptText } from "./encrypt.ts";
4import { decryptText } from "./decrypt.ts";
5import { sha3_512 } from "@noble/hashes/sha3.js";
6
7Deno.test({
8 name: "decrypts an encrypted value",
9 fn() {
10 const keys = generateKeys();
11 const text = "Hello, world!";
12 const encrypted = encryptText(keys.publicKey, text);
13 const decrypted = decryptText(keys.secretKey, encrypted);
14
15 expect(decrypted).toEqual(text);
16 },
17});
18
19Deno.test({
20 name: "errors when provided an incorrect hash",
21 fn() {
22 const keys = generateKeys();
23 const text = "Hello, world!";
24 const encrypted = encryptText(keys.publicKey, text);
25
26 encrypted.hash = sha3_512(new Uint8Array(24)).toBase64();
27
28 expect(() => decryptText(keys.secretKey, encrypted)).toThrow();
29 },
30});
31
32Deno.test({
33 name: "errors when provided an incorrect content length",
34 fn() {
35 const keys = generateKeys();
36 const text = "Hello, world!";
37 const encrypted = encryptText(keys.publicKey, text);
38
39 encrypted.length = Math.round(Math.random() * 1000);
40
41 expect(() => decryptText(keys.secretKey, encrypted)).toThrow();
42 },
43});