forked from utily/cryptly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithm.spec.ts
More file actions
62 lines (61 loc) · 2.71 KB
/
Algorithm.spec.ts
File metadata and controls
62 lines (61 loc) · 2.71 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
import * as cryptly from "./index"
describe("Algorithm", () => {
it("generate AES CBC 256", async () => {
const algorithm = cryptly.Algorithm.aesCbc(256)
const key = await algorithm.export()
expect(key).toHaveLength(43)
})
it("generate AES GCM 256", async () => {
const algorithm = cryptly.Algorithm.aesGcm(256)
const key = await algorithm.export()
expect(key).toHaveLength(43)
})
it("generate splitted AES CBC 256", async () => {
const algorithm = cryptly.Algorithm.aesCbc(256)
const keys = await algorithm.export(2)
expect(keys).toHaveLength(2)
})
it("encrypt & decrypt AES CBC", async () => {
const algorithm = cryptly.Algorithm.aesCbc(256)
const encrypted = await algorithm.encrypt("The meaning of life, the universe and everything.")
expect(encrypted.salt).toHaveLength(22)
expect(encrypted.value).toHaveLength(86)
const decrypted = await algorithm.decrypt(encrypted)
expect(decrypted).toEqual("The meaning of life, the universe and everything.")
})
it("encrypt & decrypt AES GCM", async () => {
const algorithm = cryptly.Algorithm.aesGcm("25ji7n8YAKwPvyWrtR3lUUbxABzAHA2zzdIG_Zb13Iw")
const encrypted = await algorithm.encrypt("The meaning of life, the universe and everything.")
expect(encrypted.salt).toHaveLength(22)
expect(encrypted.value).toHaveLength(87)
const decrypted = await algorithm.decrypt(encrypted)
expect(decrypted).toEqual("The meaning of life, the universe and everything.")
})
it("encrypt & decrypt AES CBC with fixed salt", async () => {
const algorithm = cryptly.Algorithm.aesCbc("VaVdZS5E6s9oEdLonNVTgSKv5zAW0LNvjBSdrmnEBS8")
const encrypted = await algorithm.encrypt(
"The meaning of life, the universe and everything.",
"K1LVSh3fzl_UTQl02fIMzg"
)
expect(encrypted.salt).toEqual("K1LVSh3fzl_UTQl02fIMzg")
expect(encrypted.value).toEqual(
"VSzGsuBi-Y2L719u3ALxpvnxeAjfJxwcXsSfJSRtwWYA8-qUXBeyQo6A0ZX7QkDgnTEqM70Cdb1-eVa7izHyFQ"
)
expect(await algorithm.encrypt("The meaning of life, the universe and everything.")).not.toEqual(encrypted.value)
const decrypted = await algorithm.decrypt(encrypted)
expect(decrypted).toEqual("The meaning of life, the universe and everything.")
const decrypted2 = await algorithm.decrypt(encrypted.value, encrypted.salt)
expect(decrypted2).toEqual("The meaning of life, the universe and everything.")
})
it("split keys", async () => {
const algorithm = cryptly.Algorithm.aesCbc(256)
const master = await algorithm.export()
expect(master).toHaveLength(43)
const keys = await algorithm.export(3)
expect(keys).toHaveLength(3)
keys.map(key => expect(key).toHaveLength(43))
const imported = await cryptly.Algorithm.aesCbc(keys).export()
expect(imported).toHaveLength(43)
expect(imported).toEqual(master)
})
})