groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject groovy git commit: Add DGM `md5`(closes #676)
Date Thu, 22 Mar 2018 15:58:37 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_5_X 31398781d -> b0d6872f4


Add DGM `md5`(closes #676)

(cherry picked from commit a2851b7)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/b0d6872f
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/b0d6872f
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/b0d6872f

Branch: refs/heads/GROOVY_2_5_X
Commit: b0d6872f4205e9053a286ad395a1d9f192523cfc
Parents: 3139878
Author: danielsun1106 <realbluesun@hotmail.com>
Authored: Thu Mar 22 23:54:22 2018 +0800
Committer: danielsun1106 <realbluesun@hotmail.com>
Committed: Thu Mar 22 23:58:30 2018 +0800

----------------------------------------------------------------------
 .../groovy/runtime/EncodingGroovyMethods.java   | 32 +++++++++++++++++++-
 .../runtime/EncodingGroovyMethodsTest.java      | 30 ++++++++++++++++++
 .../runtime/DefaultGroovyMethodsTest.groovy     |  4 +--
 3 files changed, 62 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/b0d6872f/src/main/java/org/codehaus/groovy/runtime/EncodingGroovyMethods.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/runtime/EncodingGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/EncodingGroovyMethods.java
index fdfc3a5..8eb846c 100644
--- a/src/main/java/org/codehaus/groovy/runtime/EncodingGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/EncodingGroovyMethods.java
@@ -26,6 +26,11 @@ 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;
+import java.security.NoSuchAlgorithmException;
 
 import static org.codehaus.groovy.runtime.EncodingGroovyMethodsSupport.TRANSLATE_TABLE;
 import static org.codehaus.groovy.runtime.EncodingGroovyMethodsSupport.TRANSLATE_TABLE_URLSAFE;
@@ -364,4 +369,29 @@ public class EncodingGroovyMethods {
 
         return bytes;
     }
-}
\ No newline at end of file
+
+    /**
+     * Calculate md5 of the CharSequence instance
+     * @return md5 value
+     * @throws NoSuchAlgorithmException if no MD5 algorithm found
+     * @since 2.5.0
+     */
+    public static String md5(CharSequence self) throws NoSuchAlgorithmException {
+        final String text = self.toString();
+
+        return md5(text.getBytes(StandardCharsets.UTF_8));
+    }
+
+    /**
+     * Calculate md5 of the byte array
+     * @return md5 value
+     * @throws NoSuchAlgorithmException if no MD5 algorithm found
+     * @since 2.5.0
+     */
+    public static String md5(byte[] self) throws NoSuchAlgorithmException {
+        MessageDigest md5 = MessageDigest.getInstance("MD5");
+        md5.update(ByteBuffer.wrap(self));
+
+        return String.format("%032x", new BigInteger(1, md5.digest()));
+    }
+}

http://git-wip-us.apache.org/repos/asf/groovy/blob/b0d6872f/src/test/java/org/codehaus/groovy/runtime/EncodingGroovyMethodsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/codehaus/groovy/runtime/EncodingGroovyMethodsTest.java b/src/test/java/org/codehaus/groovy/runtime/EncodingGroovyMethodsTest.java
new file mode 100644
index 0000000..4155a4c
--- /dev/null
+++ b/src/test/java/org/codehaus/groovy/runtime/EncodingGroovyMethodsTest.java
@@ -0,0 +1,30 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.codehaus.groovy.runtime;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class EncodingGroovyMethodsTest {
+    @Test
+    public void md5() throws Exception {
+        Assert.assertEquals("e99a18c428cb38d5f260853678922e03", EncodingGroovyMethods.md5("abc123"));
+        Assert.assertEquals("e99a18c428cb38d5f260853678922e03", EncodingGroovyMethods.md5("abc123".getBytes("UTF-8")));
+    }
+}

http://git-wip-us.apache.org/repos/asf/groovy/blob/b0d6872f/src/test/org/codehaus/groovy/runtime/DefaultGroovyMethodsTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/runtime/DefaultGroovyMethodsTest.groovy b/src/test/org/codehaus/groovy/runtime/DefaultGroovyMethodsTest.groovy
index 8c567e4..81fffa6 100644
--- a/src/test/org/codehaus/groovy/runtime/DefaultGroovyMethodsTest.groovy
+++ b/src/test/org/codehaus/groovy/runtime/DefaultGroovyMethodsTest.groovy
@@ -17,9 +17,6 @@
  *  under the License.
  */
 package org.codehaus.groovy.runtime
-
-import java.util.*
-
 /**
  * Tests for DGM methods
  */
@@ -310,4 +307,5 @@ class DefaultGroovyMethodsTest extends GroovyTestCase {
         assertTrue(DefaultGroovyMethods.implies(false, null))
         assertFalse(DefaultGroovyMethods.implies(true, null))
     }
+
 }


Mime
View raw message