-
Notifications
You must be signed in to change notification settings - Fork 2
/
sha2.cljc
32 lines (26 loc) · 930 Bytes
/
sha2.cljc
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
(ns fluree.crypto.sha2
(:refer-clojure :exclude [hash])
#?(:cljs (:require [goog.crypt.Sha256]
[goog.crypt.Sha512]))
#?(:clj (:import (java.security MessageDigest))))
#?(:clj (set! *warn-on-reflection* true))
(defn- hash
[ba ^long hash-size]
(assert (#{256 512} hash-size))
(let [digest (case hash-size
256 #?(:clj (MessageDigest/getInstance "SHA-256")
:cljs (doto (goog.crypt.Sha256.)
(.update ba)))
512 #?(:clj (MessageDigest/getInstance "SHA-512")
:cljs (doto (goog.crypt.Sha512.)
(.update ba))))]
#?(:clj (.digest digest ba)
:cljs (.digest digest))))
(defn ^:export sha2-256
"Create a sha2 hash from byte-array."
[ba]
(hash ba 256))
(defn ^:export sha2-512
"Create a sha2 hash from byte-array."
[ba]
(hash ba 512))