Skip to content

Instantly share code, notes, and snippets.

@mike-neck
Created June 17, 2011 08:13
Show Gist options
  • Save mike-neck/1031059 to your computer and use it in GitHub Desktop.
Save mike-neck/1031059 to your computer and use it in GitHub Desktop.
Generating Password
def xml = new groovy.xml.MarkupBuilder()
def introduction = {
'my-information'{
name('mike_neck')
'twitter-id'('@mike_neck')
job('SE')
}
'why-groovy'{
'I-Tweeted'('To rewrite a copy of sample code is too bore for me. So I will write it in Groovy')
'then'('Some Groovyist followed me on Twitter!')
}
'since'('2011-05-20')
today{
present('I will generates password.')
conditions{
condition('use characters defined by [a-zA-Z0-9]')
condition('more than 5 characters')
condition('less than 15 characters')
}
}
}
xml.present(introduction)
/**
* This code is written by @fumokmm
* {@see http://d.hatena.ne.jp/fumokmm/20090420/1240202410}
*/
IntRange.metaClass.define {
random{
int from = delegate.isReverse()? to : from
int to = delegate.isReverse()? from : to
int size = to - from + 1
(Math.floor(Math.random() * size) + from) as int
}
}
enum RandomChar{
LOWER_CASE{
char getChar(){
((int)'a') + (0..25).random()
}
}, UPPER_CASE{
char getChar(){
((int)'A') + (0..25).random()
}
}, NUMERO_CASE{
char getChar(){
((int)'0') + (0..9).random()
}
};
static final List<RandomChar> items = new ArrayList(EnumSet.allOf(RandomChar.class));
public static char get(){
items.get(new IntRange(0, items.size() - 1).random()).getChar()
}
}
def getPassword = {
password = ''
(6..14).random().times{
password += RandomChar.get()
}
password
}
getPassword()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment