Skip to content

Instantly share code, notes, and snippets.

@iskigow
Created August 3, 2016 20:56
Show Gist options
  • Save iskigow/2b5d7194a2cf321afc7dddd3a8322e11 to your computer and use it in GitHub Desktop.
Save iskigow/2b5d7194a2cf321afc7dddd3a8322e11 to your computer and use it in GitHub Desktop.
Groovy script for password generation
#!/usr/bin/env groovy
import org.apache.shiro.crypto.UnknownAlgorithmException
@Grab('org.apache.shiro:shiro-core:1.2.4')
import org.apache.shiro.crypto.hash.SimpleHash
private getOptions(CliBuilder cli, args) {
OptionAccessor options = cli.parse(args)
if (!options) {
return null
}
if (options.h) {
cli.usage()
return null
}
if (!options?.p) {
println('error: Missing required option: p | pass')
cli.usage()
return null
}
return options
}
def algDefault = 'SHA-512'
def cli = new CliBuilder(usage: "password.groovy [-h] -a <algorith> -s <salt> -p <password>")
cli.with {
h longOpt: "help", 'Show usage information'
a longOpt: 'algo', args: 1, argName: 'algorith', "Algorithm name to use when performing the hash (default: ${algDefault})"
s longOpt: 'salt', args: 1, argName: 'salt', 'The salt to use for the hash'
p longOpt: 'pass', args: 1, argName: 'password', 'Password to be hashed'
}
def options = getOptions(cli, this.args)
if (!options) return
def algo = options.a ?: algDefault
def salt = options.s ?: ''
def pass = options.p
def hash
try {
hash = new SimpleHash(algo, pass, salt).toHex()
} catch (UnknownAlgorithmException ignored) {
println("Algorithm ${algo}, not suported")
return
}
println("Algorithm: '${algo}'")
println("Salt: '${salt}'")
println("Password: '${pass}'")
println("Hash: '${hash}'")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment