-
Notifications
You must be signed in to change notification settings - Fork 1
/
algo_js.cljs
56 lines (52 loc) · 1.54 KB
/
algo_js.cljs
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
(ns uniformity.internals.crypto.algo-js
(:require ["asmcrypto.js" :as asmcrypto]
[uniformity.util :as util]))
(defn pbkdf2-hmac-sha256
^js/Uint8Array
[password
^js/Uint8Array salt
^number iterations
^number key-length-bits]
{:pre [(or
(string? password)
(instance? js/Uint8Array password))
(>= (.-length salt) 16)
(>= iterations 1000)
(= 0 (mod key-length-bits 8))]
:post [(= (.-length %)
(/ key-length-bits 8))]}
(let [password (if (string? password)
(util/str->utf8 password)
password)
key-length (/ key-length-bits 8)]
(asmcrypto/Pbkdf2HmacSha256 password salt iterations key-length)))
(defonce ^:private gcm-tag-length (/ 128 8))
(defonce ^:private gcm-nonce-length (/ 96 8))
(defn aes-gcm-encrypt
^js/Uint8Array
[^js/Uint8Array plaintext
^js/Uint8Array key
^js/Uint8Array nonce
& {:keys [aad] :or {aad []}}]
{:pre [(contains? #{16 24 32} (.-length key))
(= gcm-nonce-length (.-length nonce))]}
(.encrypt asmcrypto/AES_GCM
plaintext
key
nonce
aad
gcm-tag-length))
(defn aes-gcm-decrypt
^js/Uint8Array
[^js/Uint8Array ciphertext
^js/Uint8Array key
^js/Uint8Array nonce
& {:keys [aad] :or {aad []}}]
{:pre [(contains? #{16 24 32} (.-length key))
(= gcm-nonce-length (.-length nonce))]}
(.decrypt asmcrypto/AES_GCM
ciphertext
key
nonce
aad
gcm-tag-length))