groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Staffan Forsell (JIRA)" <>
Subject [jira] [Created] (GROOVY-9049) EncodingGroovyMethods.digest() truncates hashes over 128 bits starting with 0's
Date Thu, 21 Mar 2019 14:46:00 GMT
Staffan Forsell created GROOVY-9049:

             Summary: EncodingGroovyMethods.digest() truncates hashes over 128 bits starting
with 0's
                 Key: GROOVY-9049
             Project: Groovy
          Issue Type: Bug
          Components: groovy-runtime
    Affects Versions: 2.5.4, 3.x, 2.5.x
            Reporter: Staffan Forsell

The extension method org.codehaus.groovy.runtime.EncodingGroovyMethods#digest(byte[], java.lang.String)
produces erroneous hashes for all hash functions not having 128-bits length when the resulting
hash starts with 0.

This is due to padding the resulting string with zeroes to 32 chars length. 32 chars are only
valid for 128 bit hash functions like md5. The padding should applied differently depending
on the length of the digest returned.

Of the MessageDigest built-in hash functions this affects SHA-1 and SHA-256 at least. 

Solution would be to use *[getDigestLength|]*()
or the length of the returned digest and add some proper unit tests. 


Example of erroneous hashes:
(1..100).each {
  def sha1 =  it.toString().digest("SHA-1")
  if (sha1.size() != 40) {
    println "String '$it' length: ${sha1.size()} sha1: $sha1"
Which produces:
String '9' length: 39 sha1: ade7c2cf97f75d009975f4d720d1fa6c19f4897
String '17' length: 39 sha1: 716d9708d321ffb6a00818614779e779925365c
String '28' length: 39 sha1: a57cb53ba59c46fc4b692527a38a87c78d84028
String '43' length: 39 sha1: 286dd552c9bea9a69ecb3759e7b94777635514b
String '93' length: 39 sha1: 8a35293e09f508494096c1c1b3819edb9df50db

This message was sent by Atlassian JIRA

View raw message