Liking cljdoc? Tell your friends :D

pandect

Clojars Project CI

pandect is a fast and easy-to-use Message Digest, Checksum, HMAC and Signature library for Clojure.

Usage

Note that - for versions 0.6.0 and up - to use functions based on the BouncyCastle crypto provider, you have to manually include a version of the org.bouncycastle/bcprov-jdk15on artifact. Additionally, you need to take special care when trying to create an uberjar (see here).

REPL

(require '[pandect.algo.sha1 :refer :all])

(sha1 "Hello World!")                      ;; => "2ef7bde608ce5404e97d5f042f95f89f1c232871"
(sha1 (.getBytes "Hello World!" "UTF-8"))  ;; => "2ef7bde608ce5404e97d5f042f95f89f1c232871"
(sha1 (File. "project.clj"))               ;; => "ff3b4565652aeb835edf2715b2a28586929ea4cc"
(sha1 (FileInputStream. "project.clj"))    ;; => "ff3b4565652aeb835edf2715b2a28586929ea4cc"

;; more variants
(sha1-bytes "Hello World!")                ;; => #<byte[] [B@5293b95>
(sha1-file "project.clj")                  ;; => "ff3b4565652aeb835edf2715b2a28586929ea4cc"
(sha1-file-bytes "project.clj")            ;; => #<byte[] [B@e2606c7>

HMAC functions take an additional key:

(sha1-hmac "Hello World!" "secret-key")       ;; => "399fc3d94f6df2213f92fcf2a8b6669279ef7d20"
(sha1-hmac-bytes "Hello World!" "secret-key") ;; => #<byte[] [B@602bd522>

Signing functions expect a java.security.PrivateKey, while verification works with java.security.PublicKey or java.security.cert.Certificate:

(sha1-rsa "Hello World!" private-key)                 ;; => "5866..."
(sha1-rsa-verify "Hello World!" "5866..." public-key) ;; => true

You can tune stream processing using pandect.buffer/with-buffer-size (default is 2KB):

(require '[pandect.buffer :refer [with-buffer-size]])

(with-open [in (io/input-stream "shootout/data/1mb.txt")]
  (with-buffer-size (* 512 1024)
    (sha256 in)))

If you want to hash a String using a specific encoding, you should create the respective byte array manually:

(sha1 "Hällo World!")                          ;; => "f19c05a67c3d0f297b62e868657cf177913ce02a"
(sha1 (.getBytes "Hällo World!" "ISO-8859-1")) ;; => "cfe670bd6845020f5754b19a3c0eee602043eb89"

The namespace pandect.core contains all available algorithms but for better startup/compile times, using algorithm-specific ones is recommended.

Supported Algorithms

See the generated documentation for the available functions and their parameters.

ChecksumMDxSHASHA-3RIPEMDBLAKE2Others
Adler32*MD2*SHA-1SHA3-224RIPEMD-128BLAKE2b-160SipHash-2-4+
CRC-32*MD4SHA-256SHA3-256RIPEMD-160BLAKE2b-256SipHash-4-8+
MD5SHA-384SHA3-384RIPEMD-256BLAKE2b-384Tiger192,3
SHA-512SHA3-512RIPEMD-320BLAKE2b-512Whirlpool

* not available as MAC
+ only available as MAC

Uberjars and BouncyCastle

The BouncyCastle JAR has been signed to prevent tampering - and JCE won't allow usage of any of its functions if that signature is not present. This poses a problem when creating an Uberjar since all needed class files will be extracted from their JARs and repackaged into a single one - removing any existing signature.

You'll see, for example, the following exceptions in that case:

java.security.NoSuchAlgorithmException: Algorithm (...) not available
java.security.NoSuchProviderException: JCE cannot authenticate the provider BC

Primarily, this means you have to prevent the BouncyCastle files ending up within the JAR which can be accomplished using the :provided profile in you project.clj:

:profiles
{:provided
 {:dependencies [[org.bouncycastle/bcprov-jdk15on "1.54"]}}

Now you can build your uberjar but you have to make sure that the BouncyCastle JAR is on your classpath when running it, i.e. assuming they are both in the same directory:

java -cp bcprov-jdk15on-1.54.jar:app-standalone.jar app.core

Before version 0.6.0, pandect accessed the BouncyCastle functionality directly (i.e. not via MessageDigest/getInstance or Mac/getInstance but using actual constructor calls), meaning these limitations did not apply. It is, thus, strongly recommended to do it right and upgrade to 0.6.0.

Benchmarks

You can run benchmarks on the following Message Digest implementations:

Use the following command:

lein benchmark {pandect|clj-digest|clj-message-digest}\
               {--md2|--md5|--sha1|--sha256|--sha384|--sha512}\
               [--file <Path>]

If a file is supplied a benchmark will be run hashing its contents; if no file is given, the simple string "Hello, World!" will be hashed.

Benchmark Results

Benchmarks are run using Criterium on an Intel Core i7 2.7GHz/8GB RAM machine with Oracle JDK 1.7.0u67 (64-bit).

Results obtained using pandect 0.5.0.

Input: "Hello, World!"

Librarymd2md5sha1sha256sha384sha512adler32crc32
pandect4.27µs910ns1.07µs1.27µs1.68µs1.74µs302ns315ns
clj-digest5.92µs2.62µs3.47µs4.36µs5.65µs7.09µs--
clj-message-digest28.2µs25µs30.2µs44.7µs66.4µs80.9µs--

Input: 1KB file (times include I/O)

Librarymd2md5sha1sha256sha384sha512adler32crc32
pandect122µs13.7µs14.9µs17.9µs16.5µs16.3µs9.19µs9.12µs
clj-digest128µs19.1µs20.5µs24.8µs25.1µs26.1µs--
clj-message-digest259µs149µs155µs160µs203µs221µs--

Input: 1MB file (times include I/O)

Librarymd2md5sha1sha256sha384sha512adler32crc32
pandect112ms3.14ms4.57ms6.74ms5.1ms5.11ms582µs833µs
clj-digest113ms4.18ms5.81ms7.87ms6.15ms6.23ms--
clj-message-digest112ms3.07ms4.59ms6.73ms5.17ms5.19ms--

Contributors

License

MIT License

Copyright (c) 2014-2021 Yannick Scherer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Can you improve this documentation?Edit on GitHub

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

× close