Wednesday, July 24, 2013

Random String generator

//see: http://groovyconsole.appspot.com/edit/1032002

String generateToken(Integer size = 16){
    String availableChars = 'ABCDEFGHIJLMNOPQRSTUVXZYabcdefghijlmnopqrstuvxzy0123456789!@#$%*_'
    Random generator = new Random()
    String token = ""
    size.times {
        token += availableChars[generator.nextInt(availableChars.length() - 1)]
    }
    return token
}
 
generateToken()
generateToken(8)

No comments:

Post a Comment