-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
75 lines (70 loc) · 1.86 KB
/
test.ts
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
import {
assert,
assertEquals,
assertNotEquals,
} from "https://deno.land/std@0.208.0/assert/mod.ts";
import {
decryptFromHex,
encryptToHex,
isUUID,
IV,
Key,
randomUUID,
sha256Hex,
} from "./mod.ts";
/**
* uuid round trip
*/
Deno.test("uuid", () => {
assert(isUUID(randomUUID()), "uuid round trip");
assert(!isUUID(randomUUID() + randomUUID()), "not a uuid");
});
/**
* hex encode to a known value
*/
Deno.test("sha256Hex", async () => {
const h = await sha256Hex("hello world");
assertEquals(
h,
"b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
"sha256hex",
);
});
/**
* round trip a key
*/
Deno.test("key", async () => {
const k0 = await Key.generate();
// export key to hex
const h0 = await k0.toHex();
// import that to a new key
const k1 = await Key.fromHex(h0);
// export again
const h1 = await k1.toHex();
assertEquals(h0, h1, "key round trip");
});
/**
* round trip an IV
*/
Deno.test("iv", async () => {
const iv = await IV.fromString("hello world");
assertEquals(iv.bytes.length, IV.Length, "iv length");
const hexIV = iv.toHex();
const ivFromHex = IV.fromHex(hexIV);
assertEquals(ivFromHex.bytes, iv.bytes, "iv round trip");
assertEquals(ivFromHex.toHex(), hexIV, "hex round trip");
});
/**
* encrypt/decrypt
*/
Deno.test("encrypt/decrypt", async () => {
const k = await Key.generate();
const clearText = "hello world";
const hexCryptedWithIV = await encryptToHex(clearText, k);
const decrypted = await decryptFromHex(hexCryptedWithIV, k);
assertEquals(decrypted, clearText, "round trip encrypt decrypt");
const hexCryptedWithIV2 = await encryptToHex(clearText, k);
assertNotEquals(hexCryptedWithIV, hexCryptedWithIV2, "different iv");
const decrypted2 = await decryptFromHex(hexCryptedWithIV, k);
assertEquals(decrypted2, clearText, "round trip encrypt decrypt");
});