Skip to content

Instantly share code, notes, and snippets.

@ikarius
Created February 9, 2010 09:57
Show Gist options
  • Save ikarius/299062 to your computer and use it in GitHub Desktop.
Save ikarius/299062 to your computer and use it in GitHub Desktop.
How to generate a MD5 hash in Groovy ...
def generateMD5(String s) {
MessageDigest digest = MessageDigest.getInstance("MD5")
digest.update(s.bytes);
new BigInteger(1, digest.digest()).toString(16).padLeft(32, '0')
}
@azenla
Copy link

azenla commented Mar 19, 2014

I feel like the Groovy Compiler should error when you have unnecessary ;'s. lololololol

@heitara
Copy link

heitara commented Apr 1, 2014

To use it in gradle build script just add the missing import at the top of the file:
import java.security.MessageDigest;

@aaronzirbes
Copy link

@kaendfinger If it's in a gradle build, you can use CodeNarc to check for that type of stuff.

@mariogarcia
Copy link

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()
}

@diroussel
Copy link

In Groovy 2.5 you can just do:

'Hello'.md5()

@diroussel
Copy link

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