Liking cljdoc? Tell your friends :D

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.clj is a port of the secrets module from Python's standard library for Clojure.


test codecov Clojars Version Cljdoc Badge



Add the following dependency to your project.clj file:

[likid_geimfari/secrets "2.1.0"]

Clojure CLI/deps.edn:

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


You can find the complete documentation on the cljdoc.

Usage examples

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/randbits 32)

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

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

user=> (

user=> (

See «Recipes and best practices» for more usage examples.

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]]
    [secrets.constants :only [ascii-letters digits]]))

(defn generate-password [n]
  (join "" (secrets.core/choices (str ascii-letters digits)) n))> (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]
        [clojure.string :only [join lower-case split-lines]))

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

(defn generate-passphrase [n]
  (-> (join " " (secrets.core/choices words n))
      (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.

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close