1 1
(ns caesium.crypto.pwhash
2
  (:refer-clojure :exclude [bytes hash])
3
  (:require [caesium.binding :as b]
4
            [caesium.byte-bufs :as bb]
5
            [caesium.util :as u]
6
            [medley.core :as m]))
7

8 1
(b/defconsts [alg-argon2i13
9
              alg-argon2id13
10
              alg-default
11
              bytes-min
12
              bytes-max
13
              passwd-min
14
              passwd-max
15
              saltbytes
16
              strbytes
17
              strprefix
18
              opslimit-min
19
              opslimit-max
20
              memlimit-min
21
              memlimit-max
22
              opslimit-interactive
23
              memlimit-interactive
24
              opslimit-moderate
25
              memlimit-moderate
26
              opslimit-sensitive
27
              memlimit-sensitive
28
              primitive
29

30
              argon2i-alg-argon2i13
31
              argon2i-bytes-min
32
              argon2i-bytes-max
33
              argon2i-passwd-min
34
              argon2i-passwd-max
35
              argon2i-saltbytes
36
              argon2i-strbytes
37
              argon2i-strprefix
38
              argon2i-opslimit-min
39
              argon2i-opslimit-max
40
              argon2i-memlimit-min
41
              argon2i-memlimit-max
42
              argon2i-opslimit-interactive
43
              argon2i-memlimit-interactive
44
              argon2i-opslimit-moderate
45
              argon2i-memlimit-moderate
46
              argon2i-opslimit-sensitive
47
              argon2i-memlimit-sensitive
48

49
              argon2id-alg-argon2id13
50
              argon2id-bytes-min
51
              argon2id-bytes-max
52
              argon2id-passwd-min
53
              argon2id-passwd-max
54
              argon2id-saltbytes
55
              argon2id-strbytes
56
              argon2id-strprefix
57
              argon2id-opslimit-min
58
              argon2id-opslimit-max
59
              argon2id-memlimit-min
60
              argon2id-memlimit-max
61
              argon2id-opslimit-interactive
62
              argon2id-memlimit-interactive
63
              argon2id-opslimit-moderate
64
              argon2id-memlimit-moderate
65
              argon2id-opslimit-sensitive
66
              argon2id-memlimit-sensitive
67

68
              scryptsalsa208sha256-bytes-min
69
              scryptsalsa208sha256-bytes-max
70
              scryptsalsa208sha256-passwd-min
71
              scryptsalsa208sha256-passwd-max
72
              scryptsalsa208sha256-saltbytes
73
              scryptsalsa208sha256-strbytes
74
              scryptsalsa208sha256-strprefix
75
              scryptsalsa208sha256-opslimit-min
76
              scryptsalsa208sha256-opslimit-max
77
              scryptsalsa208sha256-memlimit-min
78
              scryptsalsa208sha256-memlimit-max
79
              scryptsalsa208sha256-opslimit-interactive
80
              scryptsalsa208sha256-memlimit-interactive
81
              scryptsalsa208sha256-opslimit-sensitive
82
              scryptsalsa208sha256-memlimit-sensitive])
83

84 1
(defn pwhash-to-buf!
85
  [buf msg salt opslimit memlimit alg]
86 1
  (b/call! pwhash buf msg salt opslimit memlimit alg)
87 1
  buf)
88

89 1
(defn pwhash
90
  "hashes a given password using default method"
91
  [key-size msg salt opslimit memlimit alg]
92 1
  (let [buf (bb/alloc key-size)]
93 1
    (pwhash-to-buf!
94 1
     buf
95 1
     (bb/->indirect-byte-buf msg)
96 1
     (bb/->indirect-byte-buf salt)
97 1
     opslimit memlimit alg)
98 1
    (bb/->bytes buf)))
99

100 1
(defn pwhash-str-to-buf!
101
  [buf msg opslimit memlimit]
102 1
  (b/call! pwhash-str buf msg opslimit memlimit)
103 1
  buf)
104

105 1
(defn pwhash-str
106
  "returns a string hash complete with all information required to verify"
107
  [msg opslimit memlimit]
108 1
  (let [buf (bb/alloc strbytes)]
109 1
    (pwhash-str-to-buf!
110 1
     buf
111 1
     (bb/->indirect-byte-buf msg)
112 1
     opslimit memlimit)
113 1
    (String. (bb/->bytes buf))))
114

115 1
(defn pwhash-str-alg-to-buf!
116
  [buf msg opslimit memlimit alg]
117 1
  (b/call! pwhash-str-alg buf msg opslimit memlimit alg)
118 1
  buf)
119

120 1
(defn pwhash-str-alg
121
  [msg opslimit memlimit alg]
122 1
  (let [buf (bb/alloc strbytes)]
123 1
    (pwhash-str-alg-to-buf!
124 1
     buf
125 1
     (bb/->indirect-byte-buf msg)
126 1
     opslimit memlimit alg)
127 1
    (String. (bb/->bytes buf))))
128

129 1
(defn pwhash-str-verify
130
  [hashpass passwd]
131 1
  (let [buf (bb/->indirect-byte-buf hashpass)
132 1
        msg (bb/->indirect-byte-buf passwd)]
133 1
    (b/call! pwhash-str-verify buf msg)))
134

135 1
(defn str-needs-rehash
136
  [hashpass opslimit memlimit]
137 1
  (let [buf (bb/->indirect-byte-buf hashpass)]
138 1
    (b/call! str-needs-rehash buf opslimit memlimit)))
139

140 1
(defn pwhash-argon2i-to-buf!
141
  [buf msg salt opslimit memlimit alg]
142 1
  (b/call! pwhash-argon2i buf msg salt opslimit memlimit alg)
143 1
  buf)
144

145 1
(defn argon2i-str-to-buf!
146
  [buf msg opslimit memlimit]
147 1
  (b/call! pwhash-argon2i-str buf msg opslimit memlimit)
148 1
  buf)
149

150 1
(defn argon2i
151
  "hashes a given password using argon2i"
152
  [key-size msg salt opslimit memlimit alg]
153 1
  (let [buf (bb/alloc key-size)]
154 1
    (pwhash-argon2i-to-buf!
155 1
     buf
156 1
     (bb/->indirect-byte-buf msg)
157 1
     (bb/->indirect-byte-buf salt)
158 1
     opslimit memlimit alg)
159 1
    (bb/->bytes buf)))
160

161 1
(defn argon2i-str
162
  [msg opslimit memlimit]
163 1
  (let [buf (bb/alloc strbytes)]
164 1
    (argon2i-str-to-buf!
165 1
     buf
166 1
     (bb/->indirect-byte-buf msg)
167 1
     opslimit memlimit)
168 1
    (String. (bb/->bytes buf))))
169

170 1
(defn argon2i-str-verify
171
  [hashpass passwd]
172 1
  (let [buf (bb/->indirect-byte-buf hashpass)
173 1
        msg (bb/->indirect-byte-buf passwd)]
174 1
    (b/call! argon2i-str-verify buf msg)))
175

176 1
(defn argon2i-str-needs-rehash
177
  [hashpass opslimit memlimit]
178 1
  (let [buf (bb/->indirect-byte-buf hashpass)]
179 1
    (b/call! argon2i-str-needs-rehash buf opslimit memlimit)))
180

181 1
(defn argon2id-to-buf!
182
  [buf msg salt opslimit memlimit alg]
183 1
  (b/call! pwhash-argon2id buf msg salt opslimit memlimit alg)
184 1
  buf)
185

186 1
(defn argon2id
187
  "hashes a given password using argon2id"
188
  [key-size msg salt opslimit memlimit alg]
189 1
  (let [buf (bb/alloc key-size)]
190 1
    (argon2id-to-buf!
191 1
     buf
192 1
     (bb/->indirect-byte-buf msg)
193 1
     (bb/->indirect-byte-buf salt)
194 1
     opslimit memlimit alg)
195 1
    (bb/->bytes buf)))
196

197 1
(defn argon2id-str-to-buf!
198
  [buf msg opslimit memlimit]
199 1
  (b/call! pwhash-argon2id-str buf msg opslimit memlimit)
200 1
  buf)
201

202 1
(defn argon2id-str
203
  [msg opslimit memlimit]
204 1
  (let [buf (bb/alloc strbytes)]
205 1
    (argon2id-str-to-buf!
206 1
     buf
207 1
     (bb/->indirect-byte-buf msg)
208 1
     opslimit memlimit)
209 1
    (String. (bb/->bytes buf))))
210

211 1
(defn argon2id-str-verify
212
  [hashpass passwd]
213 1
  (let [buf (bb/->indirect-byte-buf hashpass)
214 1
        msg (bb/->indirect-byte-buf passwd)]
215 1
    (b/call! argon2id-str-verify buf msg)))
216

217 1
(defn argon2id-str-needs-rehash
218
  [hashpass opslimit memlimit]
219 1
  (let [buf (bb/->indirect-byte-buf hashpass)]
220 1
    (b/call! argon2id-str-needs-rehash buf opslimit memlimit)))
221

222 1
(defn scryptsalsa208sha256-to-buf!
223
  [buf msg salt opslimit memlimit]
224 1
  (b/call! pwhash-scryptsalsa208sha256 buf msg salt opslimit memlimit)
225 1
  buf)
226

227 1
(defn scryptsalsa208sha256
228
  "hashes a given password using scryptsalsa208sha256"
229
  [key-size msg salt opslimit memlimit]
230 1
  (let [buf (bb/alloc key-size)]
231 1
    (scryptsalsa208sha256-to-buf!
232 1
     buf
233 1
     (bb/->indirect-byte-buf msg)
234 1
     (bb/->indirect-byte-buf salt)
235 1
     opslimit memlimit)
236 1
    (bb/->bytes buf)))
237

238 1
(defn scryptsalsa208sha256-str-to-buf!
239
  [buf msg opslimit memlimit]
240 1
  (b/call! pwhash-scryptsalsa208sha256-str buf msg opslimit memlimit)
241 1
  buf)
242

243 1
(defn scryptsalsa208sha256-str
244
  [msg opslimit memlimit]
245 1
  (let [buf (bb/alloc strbytes)]
246 1
    (scryptsalsa208sha256-str-to-buf!
247 1
     buf
248 1
     (bb/->indirect-byte-buf msg)
249 1
     opslimit memlimit)
250 1
    (String. (bb/->bytes buf))))
251

252 1
(defn scryptsalsa208sha256-str-verify
253
  [hashpass passwd]
254 1
  (let [buf (bb/->indirect-byte-buf hashpass)
255 1
        msg (bb/->indirect-byte-buf passwd)]
256 1
    (b/call! scryptsalsa208sha256-str-verify buf msg)))
257

258 1
(defn scryptsalsa208sha256-str-needs-rehash
259
  [hashpass opslimit memlimit]
260 1
  (let [buf (bb/->indirect-byte-buf hashpass)]
261 1
    (b/call!  scryptsalsa208sha256-str-needs-rehash buf opslimit memlimit)))

Read our documentation on viewing source code .

Loading