groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nelson, Erick [HDS]" <Erick.Nel...@hdsupply.com>
Subject RE: Groovy Hash Calculations
Date Fri, 04 Mar 2016 18:03:13 GMT
I  had to do this in one of my apps.
I used  java’s nameUUIDFromBytes from java.util

UUID.nameUUIDFromBytes(map.v as byte[]).toString()

If I read the docs right, it uses sha-1 under the hood.

Erick Nelson
Senior Developer | IT Application Development
HD Supply Facilities Maintenance
O: 858-831-2209
C: 760-473-7542
H: 760-930-0461
Erick.Nelson@hdsupply.com<mailto:Billy.Polihronakis@hdsupply.com>

From: Winnebeck, Jason [mailto:Jason.Winnebeck@windstream.com]
Sent: Friday, March 04, 2016 6:40 AM
To: users@groovy.apache.org
Subject: RE: Groovy Hash Calculations

Here is how I would do it. I provide both a “short” solution and a “long” one. I would
use the “short” solution if I was making for example a developer only tool and I knew
I was only hashing small things for example a configuration file in a build script and want
a one-liner. Otherwise I’d use the “long” version.

//If the content is guaranteed to be short:
def content = new ByteArrayInputStream("Here be dragons".bytes)
println MessageDigest.getInstance("SHA1").digest(content.bytes).encodeHex()

//If the content might be arbitrarily long:
content = new ByteArrayInputStream("Here be dragons".bytes)
def digest = MessageDigest.getInstance("SHA1")
content.eachByte(4096) { bytes, len ->
  digest.update(bytes, 0, len)
}
println digest.digest().encodeHex()

Jason

From: Gerald Wiltse [mailto:jerrywiltse@gmail.com]
Sent: Friday, March 04, 2016 9:18 AM
To: users@groovy.apache.org<mailto:users@groovy.apache.org>
Subject: Groovy Hash Calculations

Hello All,

I have this block, it's pretty compressed, just wondering if there is a more groovy way to
handle reading the buffer and computing the hash.

        def messageDigest = MessageDigest.getInstance("SHA1")
        def dis = new DigestInputStream(content, messageDigest)
        byte[] buffer = new byte[1024];
        while (dis.read(buffer) != -1) {}
        def sha1Hex = new BigInteger(1, messageDigest.digest()).toString(16).padLeft(40, '0')


Gerald R. Wiltse
jerrywiltse@gmail.com<mailto:jerrywiltse@gmail.com>

________________________________
This email message and any attachments are for the sole use of the intended recipient(s).
Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the
intended recipient, please contact the sender by reply email and destroy all copies of the
original message and any attachments.
Mime
View raw message