I want to get sha1 of a string

594 views
Skip to first unread message

action

unread,
Mar 1, 2014, 11:26:29 AM3/1/14
to clo...@googlegroups.com
do like this:
(ns clojurewerkz.support.hashing
  (:require [clojurewerkz.support.internal :as i])
  (:import [com.google.common.hash Hashing HashFunction HashCode]))
but:
FileNotFoundException Could not locate clojurewerkz/support__init.class or cloju
rewerkz/support.clj on classpath:   clojure.lang.RT.load (RT.java:443)
how to set the dependencies, or other solution?

Think you


JPH

unread,
Mar 1, 2014, 11:28:25 AM3/1/14
to clo...@googlegroups.com
I've had good experiences with https://github.com/xsc/pandect.

You can also use Java interop like this:
https://gist.github.com/prasincs/827272

JPH

Zach Oakes

unread,
Mar 1, 2014, 1:49:03 PM3/1/14
to clo...@googlegroups.com
You can use java.security.MessageDigest. For example:

(defn create-hash
  [data-barray]
  (.digest (java.security.MessageDigest/getInstance "SHA1") data-barray))

It takes and returns a byte array, but converting from/to a string is fairly straight-forward:

(->> "Hello, World!"
     .getBytes
     create-hash
     java.math.BigInteger.
     (format "%x")
     println)

Ben Smith-Mannschott

unread,
Mar 2, 2014, 8:52:02 AM3/2/14
to clo...@googlegroups.com
(-> "Hello, World!" .getBytes create-hash ...)

Will get you the hash of the string encoded to bytes using *some random encoding*. (Whatever the platform you're currently running on defaults to.)

You should explicitly choose an encoding and stick to it. I'd suggest UTF-8 since that can encode all the code points that might show up in a Java String.

(-> "Hello, World!" (.getBytes "UTF-8") create-hash java.math.BigInteger.)

// Ben


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

action

unread,
Mar 2, 2014, 11:04:48 AM3/2/14
to clo...@googlegroups.com, j...@hackworth.be
ok,think you!

在 2014年3月2日星期日UTC+8上午12时28分25秒,JPH写道:

xavi

unread,
Jun 29, 2014, 7:57:55 AM6/29/14
to clo...@googlegroups.com, j...@hackworth.be
I found that the hexadecimal returned by Zach's solution sometimes has a "-" prefix (for example, for the "hello" string). I guess because BigInteger(byte[]) (http://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#BigInteger-byte:A-) interprets the byte array as a two's-complement binary number, so it may be negative (http://en.wikipedia.org/wiki/Two's_complement).

To always get a SHA1 without the dash/minus prefix, BigInteger(int signum, byte[] magnitude) (http://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#BigInteger-int-byte:A-) can be used, like this:

  (->> (.getBytes "hello" "UTF-8")
       (.digest (java.security.MessageDigest/getInstance "SHA1"))
       (java.math.BigInteger. 1)
       (format "%x"))

Daniel

unread,
Jun 30, 2014, 12:47:55 AM6/30/14
to clo...@googlegroups.com
+1 for Pandect. It's an excellent library which leverages bouncy castle and has had tons of top notch work put into it. Fast, well documented, and super easy to use.
Reply all
Reply to author
Forward
0 new messages