From commits-return-5937-archive-asf-public=cust-asf.ponee.io@groovy.apache.org Thu Mar 22 16:58:39 2018 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id 01E21180676 for ; Thu, 22 Mar 2018 16:58:38 +0100 (CET) Received: (qmail 7496 invoked by uid 500); 22 Mar 2018 15:58:38 -0000 Mailing-List: contact commits-help@groovy.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@groovy.apache.org Delivered-To: mailing list commits@groovy.apache.org Received: (qmail 7483 invoked by uid 99); 22 Mar 2018 15:58:38 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 22 Mar 2018 15:58:38 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id F1DA7F6527; Thu, 22 Mar 2018 15:58:37 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sunlan@apache.org To: commits@groovy.apache.org Message-Id: <9a7f06580c084f59af32f4181a7dc2b0@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: groovy git commit: Add DGM `md5`(closes #676) Date: Thu, 22 Mar 2018 15:58:37 +0000 (UTC) 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 Authored: Thu Mar 22 23:54:22 2018 +0800 Committer: danielsun1106 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)) } + }