Bug #14461 ยป rearrange_alphanumeric.patch
| lib/securerandom.rb | ||
|---|---|---|
|
"%08x-%04x-%04x-%04x-%04x%08x" % ary
|
||
|
end
|
||
|
private def gen_random(n)
|
||
|
self.bytes(n)
|
||
|
ALPHANUMERIC = [*'A'..'Z', *'a'..'z', *'0'..'9']
|
||
|
# SecureRandom.alphanumeric generates a random alphanumeric string.
|
||
|
#
|
||
|
# The argument _n_ specifies the length, in characters, of the alphanumeric
|
||
|
# string to be generated.
|
||
|
#
|
||
|
# If _n_ is not specified or is nil, 16 is assumed.
|
||
|
# It may be larger in the future.
|
||
|
#
|
||
|
# The result may contain A-Z, a-z and 0-9.
|
||
|
#
|
||
|
# p SecureRandom.alphanumeric #=> "2BuBuLf3WfSKyQbR"
|
||
|
# p SecureRandom.alphanumeric(10) #=> "i6K93NdqiH"
|
||
|
#
|
||
|
# If a secure random number generator is not available,
|
||
|
# +NotImplementedError+ is raised.
|
||
|
def alphanumeric(n=nil)
|
||
|
n = 16 if n.nil?
|
||
|
choose(ALPHANUMERIC, n)
|
||
|
end
|
||
|
|
||
|
|
||
|
# SecureRandom.choose generates a string that randomly draws from a
|
||
|
# source array of characters.
|
||
|
#
|
||
| ... | ... | |
|
result
|
||
|
end
|
||
|
ALPHANUMERIC = [*'A'..'Z', *'a'..'z', *'0'..'9']
|
||
|
# SecureRandom.alphanumeric generates a random alphanumeric string.
|
||
|
#
|
||
|
# The argument _n_ specifies the length, in characters, of the alphanumeric
|
||
|
# string to be generated.
|
||
|
#
|
||
|
# If _n_ is not specified or is nil, 16 is assumed.
|
||
|
# It may be larger in the future.
|
||
|
#
|
||
|
# The result may contain A-Z, a-z and 0-9.
|
||
|
#
|
||
|
# p SecureRandom.alphanumeric #=> "2BuBuLf3WfSKyQbR"
|
||
|
# p SecureRandom.alphanumeric(10) #=> "i6K93NdqiH"
|
||
|
#
|
||
|
# If a secure random number generator is not available,
|
||
|
# +NotImplementedError+ is raised.
|
||
|
def alphanumeric(n=nil)
|
||
|
n = 16 if n.nil?
|
||
|
choose(ALPHANUMERIC, n)
|
||
|
private def gen_random(n)
|
||
|
self.bytes(n)
|
||
|
end
|
||
|
end
|
||