Thursday, February 26, 2015

grails quickie: random key gen

Doing a hackathon project in Grails, partially as a learning exercise.

Man, it's so slow using a new environment, even one meant to be lightweight and fast like Grails!

Anyway, this came up in two projects... to generate a random unique key:

int keyLength = 8
String charset = (('a'..'z') + ('A'..'Z') + ('0'..'9')).join()
String randomString = RandomStringUtils.random(keyLength, charset.toCharArray())

when I paste that into IntelliJ, it offers to autoinsert

import org.apache.commons.lang.RandomStringUtils

which according to this SO page (probably where I got this snippet at first) is already on the Grails classpath.

No comments:

Post a Comment