-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
51 lines (47 loc) · 1.35 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
import { assertEquals } from "https://deno.land/std@0.179.0/testing/asserts.ts";
import { Crypter, IV, Key, sha256Hex } from "./mod.ts";
/**
* 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", async () => {
const crypter = await Crypter.generate();
const clearText = "hello world";
const hexCrypted = await crypter.encryptToHex(clearText);
const decrypted = await crypter.decryptFromHex(hexCrypted);
assertEquals(decrypted, clearText, "round trip encrypt decrypt");
});