Created
February 9, 2010 09:57
-
-
Save ikarius/299062 to your computer and use it in GitHub Desktop.
How to generate a MD5 hash in Groovy ...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def generateMD5(String s) { | |
MessageDigest digest = MessageDigest.getInstance("MD5") | |
digest.update(s.bytes); | |
new BigInteger(1, digest.digest()).toString(16).padLeft(32, '0') | |
} |
To use it in gradle build script just add the missing import at the top of the file:
import java.security.MessageDigest;
@kaendfinger If it's in a gradle build, you can use CodeNarc to check for that type of stuff.
I had to do this long time ago and I came up with the following one-liner. I hope this helps:
import java.security.MessageDigest def generateMD5_A(String s){ MessageDigest.getInstance("MD5").digest(s.bytes).encodeHex().toString() }
In Groovy 2.5 you can just do:
'Hello'.md5()
In Groovy 2.5 you can just do:
'Hello'.md5()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I feel like the Groovy Compiler should error when you have unnecessary ;'s. lololololol