Repository: groovy Updated Branches: refs/heads/master 8cb379349 -> ea4165007
Add DGM `digest`(closes #677) Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/ea416500 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/ea416500 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/ea416500 Branch: refs/heads/master Commit: ea4165007b1342d2f55331f571e1161962697c89 Parents: 8cb3793 Author: Daniel Sun <[email protected]> Authored: Fri Mar 23 18:37:43 2018 +0800 Committer: sunlan <[email protected]> Committed: Fri Mar 23 19:09:57 2018 +0800 ---------------------------------------------------------------------- .../groovy/runtime/EncodingGroovyMethods.java | 37 +++++++++++++++----- .../runtime/EncodingGroovyMethodsTest.java | 6 ++++ 2 files changed, 35 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/ea416500/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 8eb846c..1fabef1 100644 --- a/src/main/java/org/codehaus/groovy/runtime/EncodingGroovyMethods.java +++ b/src/main/java/org/codehaus/groovy/runtime/EncodingGroovyMethods.java @@ -43,10 +43,9 @@ import static org.codehaus.groovy.runtime.EncodingGroovyMethodsSupport.TRANSLATE public class EncodingGroovyMethods { private static final char[] T_TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".toCharArray(); - private static final char[] T_TABLE_URLSAFE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=".toCharArray(); - private static final String CHUNK_SEPARATOR = "\r\n"; + private static final String MD5 = "MD5"; /** * Produce a Writable object which writes the Base64 encoding of the byte array. @@ -373,23 +372,45 @@ public class EncodingGroovyMethods { /** * Calculate md5 of the CharSequence instance * @return md5 value - * @throws NoSuchAlgorithmException if no MD5 algorithm found + * @throws NoSuchAlgorithmException if MD5 algorithm not 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)); + return digest(self, MD5); } /** * Calculate md5 of the byte array * @return md5 value - * @throws NoSuchAlgorithmException if no MD5 algorithm found + * @throws NoSuchAlgorithmException if MD5 algorithm not found * @since 2.5.0 */ public static String md5(byte[] self) throws NoSuchAlgorithmException { - MessageDigest md5 = MessageDigest.getInstance("MD5"); + return digest(self, MD5); + } + + /** + * digest the CharSequence instance + * @param algorithm the name of the algorithm requested, e.g. MD5, SHA-1, SHA-256, etc. + * @return digested value + * @throws NoSuchAlgorithmException if the algorithm not found + * @since 2.5.0 + */ + public static String digest(CharSequence self, String algorithm) throws NoSuchAlgorithmException { + final String text = self.toString(); + + return digest(text.getBytes(StandardCharsets.UTF_8), algorithm); + } + + /** + * digest the byte array + * @param algorithm the name of the algorithm requested, e.g. MD5, SHA-1, SHA-256, etc. + * @return digested value + * @throws NoSuchAlgorithmException if the algorithm not found + * @since 2.5.0 + */ + public static String digest(byte[] self, String algorithm) throws NoSuchAlgorithmException { + MessageDigest md5 = MessageDigest.getInstance(algorithm); md5.update(ByteBuffer.wrap(self)); return String.format("%032x", new BigInteger(1, md5.digest())); http://git-wip-us.apache.org/repos/asf/groovy/blob/ea416500/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 index 4155a4c..20c0897 100644 --- a/src/test/java/org/codehaus/groovy/runtime/EncodingGroovyMethodsTest.java +++ b/src/test/java/org/codehaus/groovy/runtime/EncodingGroovyMethodsTest.java @@ -27,4 +27,10 @@ public class EncodingGroovyMethodsTest { Assert.assertEquals("e99a18c428cb38d5f260853678922e03", EncodingGroovyMethods.md5("abc123")); Assert.assertEquals("e99a18c428cb38d5f260853678922e03", EncodingGroovyMethods.md5("abc123".getBytes("UTF-8"))); } + + @Test + public void digest() throws Exception { + Assert.assertEquals("e99a18c428cb38d5f260853678922e03", EncodingGroovyMethods.digest("abc123", "MD5")); + Assert.assertEquals("e99a18c428cb38d5f260853678922e03", EncodingGroovyMethods.digest("abc123".getBytes("UTF-8"), "MD5")); + } }
