-
Notifications
You must be signed in to change notification settings - Fork 1
/
crypt.cljs
49 lines (39 loc) · 1.08 KB
/
crypt.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
;; From cljs-hash https://github.com/davesann/cljs-hash
(ns nsfw.crypt
(:refer-clojure :exclude [hash])
(:require
[goog.crypt :as gcrypt]
[goog.crypt.Md5 :as Md5]
[goog.crypt.Sha1 :as Sha1]))
(defn string->bytes [s]
(gcrypt/stringToUtf8ByteArray s)) ;; must be utf8 byte array
(defn bytes->hex
"convert bytes to hex"
[bytes-in]
(gcrypt/byteArrayToHex bytes-in))
(defn hash-bytes [digester bytes-in]
(do
(.update digester bytes-in)
(.digest digester)))
(defn md5-
"convert bytes to md5 bytes"
[bytes-in]
(hash-bytes (goog.crypt.Md5.) bytes-in))
(defn md5-bytes
"convert utf8 string to md5 byte array"
[string]
(md5- (string->bytes string)))
(defn md5 [string]
"convert utf8 string to md5 hex string"
(bytes->hex (md5-bytes string)))
(defn sha1-
"convert bytes to sha1 bytes"
[bytes-in]
(hash-bytes (goog.crypt.Sha1.) bytes-in))
(defn sha1-bytes
"convert utf8 string to md5 byte array"
[string]
(sha1- (string->bytes string)))
(defn sha1 [string]
"convert utf8 string to sha1 hex string"
(bytes->hex (sha1-bytes string)))