Skip to content

Commit

Permalink
fix(utils): Add Secure rand nth function (#933)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xbase12 committed Aug 12, 2024
1 parent 98b419f commit 0a2623b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
4 changes: 4 additions & 0 deletions code/src/com/sixsq/nuvla/server/resources/common/utils.clj
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@
(secure-rand-int)
(+ min))))

(defn secure-rand-nth
[coll]
(nth coll (secure-rand-int (count coll))))


;;
;; utilities for handling common attributes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns com.sixsq.nuvla.server.resources.credential.key-utils
(:require
[buddy.hashers :as hashers]
[com.sixsq.nuvla.server.resources.common.utils :as u]
[clojure.string :as str])
(:import (java.io ByteArrayOutputStream DataOutputStream StringWriter)
(java.security KeyPairGenerator)
Expand Down Expand Up @@ -50,7 +51,7 @@
"Generates a random string to act as a secret API key and then returns a
tuple with that string and its digest value."
[]
(let [secret (->> (repeatedly #(rand-nth secret-chars))
(let [secret (->> (repeatedly #(u/secure-rand-nth secret-chars))
(sequence secret-xform)
(str/join "."))]
[secret (digest secret)]))
Expand Down

0 comments on commit 0a2623b

Please sign in to comment.