pandect is a fast and easy-to-use Message Digest, Checksum and HMAC library for Clojure.
Leiningen (via Clojars)
REPL
(require '[pandect.algo.sha1 :refer :all]
'[clojure.java.io :as io])
(sha1 "Hello World!") ;; => "2ef7bde608ce5404e97d5f042f95f89f1c232871"
(sha1-bytes "Hello World!") ;; => #<byte[] [B@5293b95>
(sha1 (io/file "project.clj")) ;; => "ff3b4565652aeb835edf2715b2a28586929ea4cc"
(sha1-file "project.clj") ;; => "ff3b4565652aeb835edf2715b2a28586929ea4cc"
(sha1-file-bytes "project.clj") ;; => #<byte[] [B@e2606c7>
(sha1-hmac "Hello World!" "secret-key") ;; => "399fc3d94f6df2213f92fcf2a8b6669279ef7d20"
(sha1-hmac-bytes "Hello World!" "secret-key") ;; => #<byte[] [B@602bd522>
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.
See the generated documentation for the available functions and their parameters.
Checksum | MDx | SHA | SHA-3 | RIPEMD | Others |
---|---|---|---|---|---|
Adler32* | MD2* | SHA-1 | SHA3-224 | RIPEMD-128 | SipHash-2-4+ |
CRC-32* | MD4 | SHA-256 | SHA3-256 | RIPEMD-160 | SipHash-4-8+ |
MD5 | SHA-384 | SHA3-384 | RIPEMD-256 | Tiger192,3 | |
SHA-512 | SHA3-512 | RIPEMD-320 | Whirlpool |
* not available as MAC
+ only available as MAC
You can run benchmarks on the following Message Digest implementations:
- pandect (obviously)
- clj-digest 1.4.3
- clj-message-digest 1.0.0
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.
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!"
Library | md2 | md5 | sha1 | sha256 | sha384 | sha512 | adler32 | crc32 |
---|---|---|---|---|---|---|---|---|
pandect | 4.27µs | 910ns | 1.07µs | 1.27µs | 1.68µs | 1.74µs | 302ns | 315ns |
clj-digest | 5.92µs | 2.62µs | 3.47µs | 4.36µs | 5.65µs | 7.09µs | - | - |
clj-message-digest | 28.2µs | 25µs | 30.2µs | 44.7µs | 66.4µs | 80.9µs | - | - |
Input: 1KB file (times include I/O)
Library | md2 | md5 | sha1 | sha256 | sha384 | sha512 | adler32 | crc32 |
---|---|---|---|---|---|---|---|---|
pandect | 122µs | 13.7µs | 14.9µs | 17.9µs | 16.5µs | 16.3µs | 9.19µs | 9.12µs |
clj-digest | 128µs | 19.1µs | 20.5µs | 24.8µs | 25.1µs | 26.1µs | - | - |
clj-message-digest | 259µs | 149µs | 155µs | 160µs | 203µs | 221µs | - | - |
Input: 1MB file (times include I/O)
Library | md2 | md5 | sha1 | sha256 | sha384 | sha512 | adler32 | crc32 |
---|---|---|---|---|---|---|---|---|
pandect | 112ms | 3.14ms | 4.57ms | 6.74ms | 5.1ms | 5.11ms | 582µs | 833µs |
clj-digest | 113ms | 4.18ms | 5.81ms | 7.87ms | 6.15ms | 6.23ms | - | - |
clj-message-digest | 112ms | 3.07ms | 4.59ms | 6.73ms | 5.17ms | 5.19ms | - | - |
- Yannick Scherer (@xsc)
- Toshiki Takeuchi (@totakke)
- Jan Stępień (@jstepien)
- Tom Crayford (@tcrayford)
The MIT License (MIT)
Copyright (c) 2014-2015 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.