Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: c68d3bd5f4cb
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 9cdf0f0299d7
Choose a head ref
  • 2 commits
  • 1 file changed
  • 2 contributors

Commits on Mar 17, 2018

  1. SecureRandom.alphanumeric implemented.

    [ruby-core:68098] [Feature #10849] proposed by Andrew Butterfield.
    
    SecureRandom.choose and SecureRandom.graph is not included.
    (The implementation has SecureRandom.choose but it is private.)
    
    I feel the method name, SecureRandom.choose, doesn't represent
    the behavior well.
    
    The actual use cases of SecureRandom.graph is not obvious.
    
    #4876.
    ChrisBr committed Mar 17, 2018

    Verified

    This commit was signed with the committer’s verified signature.
    headius Charles Oliver Nutter
    Copy the full SHA
    d796820 View commit details

Commits on Mar 19, 2018

  1. Merge pull request #5090 from ChrisBr/feature/secure-random

    SecureRandom.alphanumeric implemented.
    kares authored Mar 19, 2018
    Copy the full SHA
    9cdf0f0 View commit details
Showing with 42 additions and 2 deletions.
  1. +42 −2 lib/ruby/stdlib/securerandom.rb
44 changes: 42 additions & 2 deletions lib/ruby/stdlib/securerandom.rb
Original file line number Diff line number Diff line change
@@ -182,10 +182,50 @@ def uuid
"%08x-%04x-%04x-%04x-%04x%08x" % ary
end

private
def gen_random(n)
private def gen_random(n)
self.bytes(n)
end

# SecureRandom.choose generates a string that randomly draws from a
# source array of characters.
#
# The argument _source_ specifies the array of characters from which
# to generate the string.
# The argument _n_ specifies the length, in characters, of the string to be
# generated.
#
# The result may contain whatever characters are in the source array.
#
# p SecureRandom.choose([*'l'..'r']) #=> "lmrqpoonmmlqlron"
# p SecureRandom.choose([*'0'..'9'], 5) #=> "27309"
#
# If a secure random number generator is not available,
# +NotImplementedError+ is raised.
private def choose(source, n)
size = source.size
n.times.map {source[random_number(size)]}.join('')
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)
end
end

SecureRandom.extend(Random::Formatter)