-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.ts
More file actions
95 lines (77 loc) · 3.95 KB
/
index.test.ts
File metadata and controls
95 lines (77 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { describe, expect, test } from "bun:test"
import SingleCryptText, { createSymmetricKeyFromText, encryptTextSymmetrically, decryptTextSymmetrically } from "."
function randomString() {
return crypto.getRandomValues(new Uint8Array(24)).toBase64()
}
describe("Functional", () => {
test("Generate a random symmetric CryptoKey", async() => {
const key = await createSymmetricKeyFromText(randomString())
expect(key).toBeDefined()
})
test("Encrypt a random value", async() => {
const key = await createSymmetricKeyFromText(randomString())
expect(await encryptTextSymmetrically(key, randomString())).toBeString()
})
test("Decrypt a random value", async() => {
const symCryptoKey = await createSymmetricKeyFromText(randomString())
const randomValue = randomString()
const ciphertext = await encryptTextSymmetrically(symCryptoKey, randomValue)
const decrypted = await decryptTextSymmetrically(symCryptoKey, ciphertext)
expect(randomValue).toBe(decrypted)
})
test("Decrypt a random value with two different CryptoKey objects", async() => {
const randomKeyString = randomString()
const symCryptoKey = await createSymmetricKeyFromText(randomKeyString)
const symCryptoKey2 = await createSymmetricKeyFromText(randomKeyString)
const randomValue = randomString()
const ciphertext = await encryptTextSymmetrically(symCryptoKey, randomValue)
const decrypted = await decryptTextSymmetrically(symCryptoKey2, ciphertext)
expect(randomValue).toBe(decrypted)
})
test("Check if encrypting and decrypting with different CryptoKey objects returns an error", async() => {
const symCryptoKey = await createSymmetricKeyFromText(randomString())
const symCryptoKey2 = await createSymmetricKeyFromText(randomString())
const randomValue = randomString()
const ciphertext = await encryptTextSymmetrically(symCryptoKey, randomValue)
await expect(decryptTextSymmetrically(symCryptoKey2, ciphertext)).rejects.toThrow()
})
test("Check if disabling `urlSafe` changes the encryption results", async() => {
const symCryptoKey = await createSymmetricKeyFromText(randomString())
const randomValue = randomString()
const ciphertextUrlSafe = await encryptTextSymmetrically(symCryptoKey, randomValue, true)
const ciphertextUrlUnsafe = await encryptTextSymmetrically(symCryptoKey, randomValue, false)
expect(ciphertextUrlSafe).not.toBe(ciphertextUrlUnsafe)
const decryptedUrlSafe = await decryptTextSymmetrically(symCryptoKey, ciphertextUrlSafe)
const decryptedUrlUnafe = await decryptTextSymmetrically(symCryptoKey, ciphertextUrlUnsafe)
expect(randomValue).toBe(decryptedUrlSafe)
expect(randomValue).toBe(decryptedUrlUnafe)
})
})
describe("Object-oriented", () => {
const singleCryptText1 = new SingleCryptText(randomString())
test("Encrypt and decrypt random values", async() => {
const randomValue = randomString()
const ciphertext = await singleCryptText1.encrypt(randomValue)
const decrypted = await singleCryptText1.decrypt(ciphertext)
expect(randomValue).toBe(decrypted)
let randomValue2
do {
randomValue2 = randomString()
} while (randomValue === randomValue2)
const ciphertext2 = await singleCryptText1.encrypt(randomValue2)
expect(ciphertext).not.toBe(ciphertext2)
const decrypted2 = await singleCryptText1.decrypt(ciphertext2)
expect(decrypted).not.toBe(decrypted2)
expect(randomValue2).toBe(decrypted2)
})
test("Check if disabling `urlSafe` changes the encryption results", async() => {
const randomValue = randomString()
const ciphertext = await singleCryptText1.encrypt(randomValue)
const decrypted = await singleCryptText1.decrypt(ciphertext)
expect(randomValue).toBe(decrypted)
const ciphertext2 = await singleCryptText1.encrypt(randomValue, false)
expect(ciphertext).not.toBe(ciphertext2)
const decrypted2 = await singleCryptText1.decrypt(ciphertext2)
expect(randomValue).toBe(decrypted2)
})
})