groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [groovy] branch master updated: GROOVY-9049: EncodingGroovyMethods.digest() truncates hashes over 128 bits starting with 0's(closes #901)
Date Sat, 23 Mar 2019 15:48:31 GMT
This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new d5a0ef7  GROOVY-9049: EncodingGroovyMethods.digest() truncates hashes over 128 bits
starting with 0's(closes #901)
d5a0ef7 is described below

commit d5a0ef74b0f00ac2fd53ece74f175899e683c336
Author: Daniel Sun <sunlan@apache.org>
AuthorDate: Sat Mar 23 23:48:12 2019 +0800

    GROOVY-9049: EncodingGroovyMethods.digest() truncates hashes over 128 bits starting with
0's(closes #901)
---
 .../java/org/codehaus/groovy/runtime/EncodingGroovyMethods.java  | 3 +--
 .../org/codehaus/groovy/runtime/EncodingGroovyMethodsTest.java   | 9 +++++++++
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/runtime/EncodingGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/EncodingGroovyMethods.java
index f2ec1e2..d41f04e 100644
--- a/src/main/java/org/codehaus/groovy/runtime/EncodingGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/EncodingGroovyMethods.java
@@ -26,7 +26,6 @@ import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import java.io.Writer;
-import java.math.BigInteger;
 import java.nio.ByteBuffer;
 import java.nio.charset.StandardCharsets;
 import java.security.MessageDigest;
@@ -436,6 +435,6 @@ public class EncodingGroovyMethods {
         MessageDigest md = MessageDigest.getInstance(algorithm);
         md.update(ByteBuffer.wrap(self));
 
-        return String.format("%032x", new BigInteger(1, md.digest()));
+        return encodeHex(md.digest()).toString();
     }
 }
diff --git a/src/test/org/codehaus/groovy/runtime/EncodingGroovyMethodsTest.java b/src/test/org/codehaus/groovy/runtime/EncodingGroovyMethodsTest.java
index 527399c..423d58e 100644
--- a/src/test/org/codehaus/groovy/runtime/EncodingGroovyMethodsTest.java
+++ b/src/test/org/codehaus/groovy/runtime/EncodingGroovyMethodsTest.java
@@ -38,5 +38,14 @@ public class EncodingGroovyMethodsTest {
     public void digest() throws Exception {
         Assert.assertEquals("e99a18c428cb38d5f260853678922e03", EncodingGroovyMethods.digest("abc123",
"MD5"));
         Assert.assertEquals("e99a18c428cb38d5f260853678922e03", EncodingGroovyMethods.digest("abc123".getBytes("UTF-8"),
"MD5"));
+
+        // GROOVY-9049: EncodingGroovyMethods.digest() truncates hashes over 128 bits starting
with 0's
+        Assert.assertEquals("0ade7c2cf97f75d009975f4d720d1fa6c19f4897", EncodingGroovyMethods.digest("9",
"SHA-1"));
+        Assert.assertEquals("0716d9708d321ffb6a00818614779e779925365c", EncodingGroovyMethods.digest("17",
"SHA-1"));
+        Assert.assertEquals("0a57cb53ba59c46fc4b692527a38a87c78d84028", EncodingGroovyMethods.digest("28",
"SHA-1"));
+        Assert.assertEquals("0286dd552c9bea9a69ecb3759e7b94777635514b", EncodingGroovyMethods.digest("43",
"SHA-1"));
+        Assert.assertEquals("08a35293e09f508494096c1c1b3819edb9df50db", EncodingGroovyMethods.digest("93",
"SHA-1"));
+
+
     }
 }


Mime
View raw message