Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-9049

EncodingGroovyMethods.digest() truncates hashes over 128 bits starting with 0's

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 3.0.0-alpha-4, 2.5.6
    • Fix Version/s: 3.0.0-beta-1, 2.5.7
    • Component/s: groovy-runtime
    • Labels:
      None

      Description

      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
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                daniel_sun Daniel Sun
                Reporter:
                Stalle Staffan Forsell
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 20m
                  20m