You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2018/03/22 15:54:31 UTC
groovy git commit: Add DGM `md5`(closes #676)
Repository: groovy
Updated Branches:
refs/heads/master 94aa0db2d -> a2851b79a
Add DGM `md5`(closes #676)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/a2851b79
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/a2851b79
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/a2851b79
Branch: refs/heads/master
Commit: a2851b79a1822600ba1c42b3bc50e87e644351c4
Parents: 94aa0db
Author: danielsun1106 <re...@hotmail.com>
Authored: Thu Mar 22 23:54:22 2018 +0800
Committer: danielsun1106 <re...@hotmail.com>
Committed: Thu Mar 22 23:54:22 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/a2851b79/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/a2851b79/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/a2851b79/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))
}
+
}