A Clojure library designed to generate secure random numbers for managing secrets

authentication, clj, clojure, cryptography, generator, hacktoberfest, java, jvm, passwords, python, random, secrets, secure, security, tokens


Build Status Clojars Version Cljdoc Badge

The secrets is a library designed to generate cryptographically strong random numbers suitable for managing data such as passwords, account authentication, security tokens, and related secrets.

The secrets is an implementation of the secrets module from Python's standard library for Clojure.

See «Recipes and best practices» for usage examples.



Add the following dependency to your project.clj file:

[likid_geimfari/secrets "0.1.3"]

Clojure CLI/deps.edn:

likid_geimfari/secrets {:mvn/version "0.1.3"}


You can find the complete documentation on the cljdoc.


user=> (secrets.core/token-hex 32)

user=> (secrets.core/token-urlsafe 32)

user=> (secrets.core/token-bytes 16)
#object["[B" 0x3b2454e9 "[B@3b2454e9"]

user=> (secrets.core/randbelow 100)

user=> (secrets.core/choice [8 16 32 64 128])

user=> (secrets.core/uuid4)

There is a namespace secrets.strings with useful constants:

user=> secrets.strings/ascii-lowercase
user=> secrets.strings/ascii-uppercase
user=> secrets.strings/ascii-letters
user=> secrets.strings/digits
user=> secrets.strings/hexdigits
user=> secrets.strings/octdigits
user=> secrets.strings/punctuation

Recipes and best practices

This section shows recipes and best practices for using secrets to manage a basic level of security.

Generate an eight-character alphanumeric password:


(use '[clojure.string :only [join]])
(use '[secrets.core :only [choice]])
(use '[secrets.strings :only [ascii-letters digits]])

(defn generate-password [n]
  (join "" (repeatedly n #(choice (str ascii-letters digits)))))> (generate-password 8)

Note: Applications should not store passwords in a recoverable format, whether plain text or encrypted. They should be salted and hashed using a cryptographically-strong one-way (irreversible) hash function.

Generate an XKCD-style passphrase:


(use '[secrets.core :only [choice]])
(use '[secrets.strings :only [ascii-letters digits]])
(use '[clojure.string :only [join lower-case split-lines]])

(def words
  (-> (slurp "/usr/share/dict/words")

(defn generate-passphrase [n]
  (-> (join " " (repeatedly n #(choice words)))
      (lower-case)))> (generate-passphrase 5)
"uniaxally intercarrier straddleback basihyoid unhusk"

Generate a hard-to-guess temporary URL containing a security token suitable for password recovery applications:


(use '[secrets.core :only [token-urlsafe]])

(defn generate-password-recovery-url [n]
  (str "" (token-urlsafe n)))> (generate-password-recovery-url 32)


MIT License. See LICENSE for more information.