Skip to content

Instantly share code, notes, and snippets.

@pditommaso
Last active September 20, 2021 15:50
Show Gist options
  • Save pditommaso/cf724c1d2353342784f0 to your computer and use it in GitHub Desktop.
Save pditommaso/cf724c1d2353342784f0 to your computer and use it in GitHub Desktop.
Groovy SHA1 generator
import java.security.*
String password = "toto\n"
MessageDigest sha1 = MessageDigest.getInstance("SHA1")
byte[] digest = sha1.digest(password.getBytes())
System.out.println(new BigInteger(1, digest).toString(16))
@bitsnaps
Copy link

bitsnaps commented Aug 29, 2019

for groovy 2.5 things are much easier:

def value = 'IamASecret'
 
def md5 = value.md5()
 
// We can provide hash algorithm with digest method.
def md2 = value.digest('MD2')
def sha1 = value.digest('SHA-1')
def sha256 = value.digest('SHA-256')
def sha384 = value.digest('SHA-384')
def sha512 = value.digest('SHA-512')
assert md5 == 'a5f3147c32785421718513f38a20ca44'
assert md2 == '832cbe3966e186194b1203c00ef47488'
assert sha1 == '52ebfed118e0a411e9d9cbd60636fc9dea718928'
assert sha256 == '4f5e3d486d1fd6c822a81aa0b93d884a2a44daf2eb69ac779a91bc76de512cbe'
assert sha384 == '2a46d29c961ae809d4b7da941ca46e23f4ccbcc08186a8a6b2e79b2f3074d06221a3ab1dcd4ca3e9ffa2281da1f200fe'
assert sha512 == '9a0388c0f8dab97d1a0908706b894cd90fa0076154535922cb79943c7a8430cbd9bb7ea21cf284d6c9dc95f5b519dbec1f195f58818039f9f5cbfcacdabee13d'

example from mr. Haki's blog.

@hlwanghl
Copy link

for groovy 2.5 things are much easier:

def value = 'IamASecret'
 
def md5 = value.md5()
 
// We can provide hash algorithm with digest method.
def md2 = value.digest('MD2')
def sha1 = value.digest('SHA-1')
def sha256 = value.digest('SHA-256')
def sha384 = value.digest('SHA-384')
def sha512 = value.digest('SHA-512')
assert md5 == 'a5f3147c32785421718513f38a20ca44'
assert md2 == '832cbe3966e186194b1203c00ef47488'
assert sha1 == '52ebfed118e0a411e9d9cbd60636fc9dea718928'
assert sha256 == '4f5e3d486d1fd6c822a81aa0b93d884a2a44daf2eb69ac779a91bc76de512cbe'
assert sha384 == '2a46d29c961ae809d4b7da941ca46e23f4ccbcc08186a8a6b2e79b2f3074d06221a3ab1dcd4ca3e9ffa2281da1f200fe'
assert sha512 == '9a0388c0f8dab97d1a0908706b894cd90fa0076154535922cb79943c7a8430cbd9bb7ea21cf284d6c9dc95f5b519dbec1f195f58818039f9f5cbfcacdabee13d'

example from mr. Haki's blog.

it works, thx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment