Skip to content

Fix lein eastwood warnings and some typos #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
2018-03-23 version 1.4.8
* Minor fixes

2018-03-18 version 1.4.7
* Minor simplifications, fix typo.
* Clojure 1.9.0
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

`digest` - Message digest library for Clojure. Providing md5, sha-256, ...

There are several digest function (such as `md5`, `sha-256` ...) in this
There are several digest functions (such as `md5`, `sha-256` ...) in this
namespace. Each can handle the following input types:

* java.lang.String
Expand All @@ -27,7 +27,7 @@ namespace. Each can handle the following input types:
"163883d3e0e3b0c028d35b626b98564be8d9d649ed8adb8b929cb8c94c735c59"

# Installation
Add `[digest "1.4.6"]` to your `project.clj`.
Add `[digest "1.4.8"]` to your `project.clj`.

# License
Copyright&copy; 2017 Miki Tebeka <miki.tebeka@gmail.com>
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject digest "1.4.7"
(defproject digest "1.4.8"
:description "Digest algorithms (MD5, SHA ...) for Clojure"
:author "Miki Tebeka <miki.tebeka@gmail.com>"
:url "https://github.com/tebeka/clj-digest"
Expand Down
38 changes: 20 additions & 18 deletions src/digest.clj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
(ns digest
#^{ :author "Miki Tebeka <miki.tebeka@gmail.com>"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove the meta data (OK for my name, but the doc?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I'm sorry about removing your name and doc from require - i executed lein slamhound in project folder which optimised the require but also removed the meta data. Will restore it!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(:require .. :refer ..) is a new way to do the same thing that allows effectively deprecate :use, which has some drawbacks. Please see https://stackoverflow.com/questions/871997/difference-between-use-and-require#comment25370819_16429572 for more details.

:doc "Message digest algorithms for Clojure"}
(:use [clojure.string :only (split lower-case)])
(:import java.util.Arrays
(java.security MessageDigest Security Provider)
(java.io FileInputStream File InputStream)))
#^{:author "Miki Tebeka <miki.tebeka@gmail.com>"
:doc "Message digest algorithms for Clojure"}
(:require [clojure.string :refer [join lower-case split]])
(:import (java.io File FileInputStream InputStream)
(java.security MessageDigest Provider Security)
(java.util Arrays)))

; Default buffer size for reading
(def ^:dynamic *buffer-size* 1024)
Expand All @@ -13,7 +13,7 @@
"Read some data from reader. Return [data size] if there's more to read,
otherwise nil."
[^InputStream reader]
(let [^bytes buffer (make-array Byte/TYPE *buffer-size*)
(let [^bytes buffer (make-array Byte/TYPE *buffer-size*)
size (.read reader buffer)]
(when (pos? size)
(if (= size *buffer-size*) buffer (Arrays/copyOf buffer size)))))
Expand All @@ -28,21 +28,21 @@
[^MessageDigest algorithm]
(let [size (* 2 (.getDigestLength algorithm))
sig (.toString (BigInteger. 1 (.digest algorithm)) 16)
padding (apply str (repeat (- size (count sig)) "0"))]
padding (join (repeat (- size (count sig)) "0"))]
(str padding sig)))

(defprotocol Digestible
(-digest [message algorithm]))

(extend-protocol Digestible
(class (make-array Byte/TYPE 0))
(-digest [message algorithm]
(-digest [message algorithm]
(-digest [message] algorithm))

java.util.Collection
;; Code "borrowed" from
;; Code "borrowed" from
;; * http://www.holygoat.co.uk/blog/entry/2009-03-26-1
;; * http://www.rgagnon.com/javadetails/java-0416.html
;; * http://www.rgagnon.com/javadetails/java-0416.html
(-digest [message algorithm]
(let [^MessageDigest algo (MessageDigest/getInstance algorithm)]
(.reset algo)
Expand All @@ -52,11 +52,11 @@
String
(-digest [message algorithm]
(-digest [(.getBytes message)] algorithm))

InputStream
(-digest [reader algorithm]
(-digest (byte-seq reader) algorithm))

File
(-digest [file algorithm]
(with-open [f (FileInputStream. file)]
Expand All @@ -71,9 +71,10 @@
[algorithm message]
(-digest message algorithm))

(defn algorithms []
(defn algorithms
"List support digest algorithms."
(let [providers (into [] (Security/getProviders))
[]
(let [providers (vec (Security/getProviders))
names (mapcat (fn [^Provider p] (enumeration-seq (.keys p))) providers)
digest-names (filter #(re-find #"MessageDigest\.[A-Z0-9-]+$" %) names)]
(set (map #(last (split % #"\.")) digest-names))))
Expand All @@ -89,11 +90,12 @@
(partial digest algorithm-name))
(alter-meta! update-meta))))

(defn- create-fns []
(defn- create-fns
"Create utility function for each digest algorithms.
For example will create an md5 function for MD5 algorithm."
[]
(doseq [algorithm (algorithms)]
(create-fn! algorithm)))

; Create utility functions such as md5, sha-2 ...
; Create utility functions such as md5, sha-256 ...
(create-fns)
12 changes: 6 additions & 6 deletions test/digest_test.clj
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
(ns digest-test
(:require [clojure.string :refer [lower-case includes?]]
[clojure.test :refer :all]
[digest :refer :all :reload-all true])
[digest :refer :all])
(:import java.io.File))

(deftest md5-test
(is (= (digest "md5" "foo") "acbd18db4cc2f85cedef654fccc4a4d8")))

(deftest sha-256-test
(is (= (sha-256 "foo")
(is (= (sha-256 "foo")
"2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae")))

(deftest algorithms-test
(let [names (algorithms)]
(is not (empty? names))
(is (not (empty? names)))
(is (names "SHA-1"))))

(deftest utils-test
Expand All @@ -27,7 +27,7 @@
(:arglists (meta #'md5)))))

(def ^:dynamic *logo-md5* "38cf20fa3c9dc72be56965eb1c311dfa")
(def ^:dynamic *logo-sha256*
(def ^:dynamic *logo-sha256*
"42c2af2a0509832f39d0cef3ecd1612b7857c55abbe2170470eabb2a0318701c")

(deftest file-test
Expand All @@ -40,5 +40,5 @@
(md5 nil))

(deftest length-test
(is (= (sha (File. "test/length.txt"))
"007b65165b253172d054189e8e3175f3bcb9e28e")))
(is (= (sha (File. "test/length.txt"))
"007b65165b253172d054189e8e3175f3bcb9e28e")))