2021-11-19 17:37:08 +00:00
|
|
|
|
use str
|
|
|
|
|
use re
|
|
|
|
|
|
2021-12-11 21:09:02 +00:00
|
|
|
|
fn -bytes {|begin size|
|
2021-11-19 17:37:08 +00:00
|
|
|
|
str:from-codepoints (range $begin (+ $begin $size))
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-11 21:09:02 +00:00
|
|
|
|
fn -upper {||
|
2021-11-19 17:37:08 +00:00
|
|
|
|
-bytes 0x41 26
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-11 21:09:02 +00:00
|
|
|
|
fn -lower {||
|
2021-11-19 17:37:08 +00:00
|
|
|
|
-bytes 0x61 26
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-11 21:09:02 +00:00
|
|
|
|
fn -digit {||
|
2021-11-19 17:37:08 +00:00
|
|
|
|
-bytes 0x30 10
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-11 21:09:02 +00:00
|
|
|
|
fn -j {|@strings|
|
2021-11-19 17:37:08 +00:00
|
|
|
|
str:join '' $strings
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-11 21:09:02 +00:00
|
|
|
|
fn -alpha {||
|
2021-11-19 17:37:08 +00:00
|
|
|
|
-j (-upper) (-lower)
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-11 21:09:02 +00:00
|
|
|
|
fn -alphanum {||
|
2021-11-19 17:37:08 +00:00
|
|
|
|
-j (-alpha) (-digit)
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-11 21:09:02 +00:00
|
|
|
|
fn -random {|chars|
|
2021-11-19 17:37:08 +00:00
|
|
|
|
var size = (count $chars)
|
|
|
|
|
print $chars[(randint 0 $size)]
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-11 21:09:02 +00:00
|
|
|
|
fn -is-int {|arg|
|
2021-11-19 17:37:08 +00:00
|
|
|
|
re:match '^\d+$' $arg
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-11 21:09:02 +00:00
|
|
|
|
fn -help {||
|
2021-11-19 17:37:08 +00:00
|
|
|
|
echo 'Génère une chaîne de caractères aléatoires'
|
|
|
|
|
echo 'Usage: genstr [(-auldh)] [<chars>] [<size>]'
|
|
|
|
|
echo ''
|
|
|
|
|
echo 'Options:'
|
|
|
|
|
echo ' -a Uniquement des lettres ([A-Za-z])'
|
|
|
|
|
echo ' -u Uniquement des lettres majuscules ([A-Z])'
|
|
|
|
|
echo ' -l Uniquement des lettres minuscules ([a-z])'
|
|
|
|
|
echo ' -d Uniquement des chiffres ([0-9])'
|
|
|
|
|
echo ''
|
|
|
|
|
echo '<chars> Liste de caractères possibles'
|
|
|
|
|
echo '<size> Longueur de la chaîne (par défaut: 10)'
|
|
|
|
|
echo ''
|
|
|
|
|
echo 'Si aucune classe de caractères ou liste de caractères n’est fournie, utilise les caractères alphanumériques ([A-Za-z0-9]).'
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-11 21:09:02 +00:00
|
|
|
|
edit:add-var genstr~ {|@args|
|
2021-11-19 17:37:08 +00:00
|
|
|
|
var len chars h = 0 '' $false
|
2021-12-11 21:09:02 +00:00
|
|
|
|
each {|e|
|
2021-11-19 17:37:08 +00:00
|
|
|
|
if (or (eq $e -h) (eq $e --help)) {
|
|
|
|
|
set h = $true
|
|
|
|
|
-help
|
|
|
|
|
break
|
|
|
|
|
} elif (eq $e -a) {
|
|
|
|
|
set chars = (-j $chars (-alpha))
|
|
|
|
|
} elif (eq $e -u) {
|
|
|
|
|
set chars = (-j $chars (-upper))
|
|
|
|
|
} elif (eq $e -l) {
|
|
|
|
|
set chars = (-j $chars (-lower))
|
|
|
|
|
} elif (eq $e -d) {
|
|
|
|
|
set chars = (-j $chars (-digit))
|
|
|
|
|
} elif (and (-is-int $e) (> $e 0)) {
|
|
|
|
|
set len = $e
|
|
|
|
|
} else {
|
|
|
|
|
set chars = (-j $chars $e)
|
|
|
|
|
}
|
|
|
|
|
} $args
|
|
|
|
|
|
|
|
|
|
if (not $h) {
|
|
|
|
|
if (== $len 0) {
|
|
|
|
|
set len = 10
|
|
|
|
|
}
|
|
|
|
|
if (eq $chars '') {
|
|
|
|
|
set chars = (-alphanum)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set chars = [(str:split '' $chars)]
|
|
|
|
|
|
2021-12-11 21:09:02 +00:00
|
|
|
|
range $len | each {|_|
|
2021-11-19 17:37:08 +00:00
|
|
|
|
-random $chars
|
|
|
|
|
}
|
|
|
|
|
echo
|
|
|
|
|
}
|
|
|
|
|
}
|