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";
5
6Deno.test({
7 name: "encrypts and decrypts multiple messages with the same keypair",
8 fn() {
9 const keys = generateKeys();
10
11 const text1 = "First message";
12 const text2 = "Second message";
13 const text3 = "Third message";
14
15 const encrypted1 = encryptText(keys.publicKey, text1);
16 const encrypted2 = encryptText(keys.publicKey, text2);
17 const encrypted3 = encryptText(keys.publicKey, text3);
18
19 expect(encrypted1.content.length).toBeGreaterThan(0);
20 expect(encrypted2.content.length).toBeGreaterThan(0);
21 expect(encrypted3.content.length).toBeGreaterThan(0);
22
23 const decrypted1 = decryptText(keys.secretKey, encrypted1);
24 const decrypted2 = decryptText(keys.secretKey, encrypted2);
25 const decrypted3 = decryptText(keys.secretKey, encrypted3);
26
27 expect(decrypted1).toEqual(text1);
28 expect(decrypted2).toEqual(text2);
29 expect(decrypted3).toEqual(text3);
30 },
31});
32
33Deno.test({
34 name: "encrypts messages with reused public key reference",
35 fn() {
36 const keys = generateKeys();
37 const publicKey = keys.publicKey;
38
39 const encrypted1 = encryptText(publicKey, "Message 1");
40 const encrypted2 = encryptText(publicKey, "Message 2");
41 const encrypted3 = encryptText(publicKey, "Message 3");
42
43 expect(encrypted1.cipherText).not.toEqual(encrypted2.cipherText);
44 expect(encrypted2.cipherText).not.toEqual(encrypted3.cipherText);
45 expect(encrypted1.cipherText).not.toEqual(encrypted3.cipherText);
46
47 expect(decryptText(keys.secretKey, encrypted1)).toEqual("Message 1");
48 expect(decryptText(keys.secretKey, encrypted2)).toEqual("Message 2");
49 expect(decryptText(keys.secretKey, encrypted3)).toEqual("Message 3");
50 },
51});