annotate bin/randstr @ 703:b7cd7465cc26

[bashrc] unleak variables
author nanaya <me@nanaya.net>
date Sun, 18 Dec 2022 19:29:43 +0900
parents ecff9d2c0500
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
586
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
1 #!/usr/bin/env ruby
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
2
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
3 require "securerandom"
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
4
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
5 def random_base62(length)
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
6 group = ["A".."Z", "a".."z", "0".."9"].map(&:to_a).flatten
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
7
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
8 length.times.map do |x|
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
9 group[SecureRandom.random_number(group.length)]
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
10 end.join
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
11 end
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
12
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
13 length = ARGV[0].to_i
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
14 length = 12 if length <= 0
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
15
ecff9d2c0500 Simple random string generator
nanaya <me@myconan.net>
parents:
diff changeset
16 puts random_base62(length)