-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sha.clj
26 lines (19 loc) · 739 Bytes
/
sha.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
(ns certifiable.sha
(:require [clojure.string :as string])
(:import [java.security MessageDigest]
[java.nio.charset StandardCharsets]))
;; taken from https://github.com/tebeka/clj-digest/blob/master/src/digest.clj
(defn- signature
[^MessageDigest algorithm]
(let [size (* 2 (.getDigestLength algorithm))
sig (.toString (BigInteger. 1 (.digest algorithm)) 16)
padding (string/join (repeat (- size (count sig)) "0"))]
(str padding sig)))
(defn sha-signature [s]
(let [md (MessageDigest/getInstance "SHA-256")]
(.reset md)
(.update md (.getBytes s StandardCharsets/UTF_8))
(signature md)))
(defn sha-signature-short [s]
(subs (sha-signature s) 0 7))
#_(sha-digest-short "abasdf")