Groovy File and URL MD5


/ Published in: Groovy
Save to your folder(s)

Generating MD5 HEX string from either URL or File instance in Groovy (more elegant than the Java version).


Copy this code and paste it in your HTML
  1. import java.security.MessageDigest
  2.  
  3. def generateMD5(final file) {
  4. MessageDigest digest = MessageDigest.getInstance("MD5")
  5. file.withInputStream(){is->
  6. byte[] buffer = new byte[8192]
  7. int read = 0
  8. while( (read = is.read(buffer)) > 0) {
  9. digest.update(buffer, 0, read);
  10. }
  11. }
  12. byte[] md5sum = digest.digest()
  13. BigInteger bigInt = new BigInteger(1, md5sum)
  14. return bigInt.toString(16)
  15. }

Report this snippet


Comments


You need to login to view comments.