Project

General

Profile

Feature #10849

Adding an alphanumeric function to SecureRandom

Added by andrewcbutterfield@gmail.com (Andrew Butterfield) over 4 years ago. Updated almost 2 years ago.

Status:
Closed
Priority:
Normal
Target version:
-
[ruby-core:68098]

Description

It would be handy to have a method that produces a random alphanumeric string.


Files

securerandom.txt (1.32 KB) securerandom.txt Git Diff andrewcbutterfield@gmail.com (Andrew Butterfield), 02/12/2015 11:56 PM
securerandom.patch (1.32 KB) securerandom.patch andrewcbutterfield@gmail.com (Andrew Butterfield), 02/13/2015 12:02 AM
securerandom.patch (2.49 KB) securerandom.patch andrewcbutterfield@gmail.com (Andrew Butterfield), 02/19/2015 08:14 PM
test_securerandom.patch (641 Bytes) test_securerandom.patch andrewcbutterfield@gmail.com (Andrew Butterfield), 02/19/2015 08:15 PM

Associated revisions

Revision b867882a
Added by akr (Akira Tanaka) almost 2 years ago

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.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60297 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 60297
Added by akr (Akira Tanaka) almost 2 years ago

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.

Revision 60297
Added by akr (Akira Tanaka) almost 2 years ago

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.

Revision 60297
Added by akr (Akira Tanaka) almost 2 years ago

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.

History

Updated by nobu (Nobuyoshi Nakada) over 4 years ago

The length of the result string is about 4/3 of n.

I don't like this interface.

  def self.choose(source, length)
    size = source.size
    length.times.map {source[random_number(size)]}.join('')
  end

  GRAPH = [*'!'..'~']
  def self.graph(length)
    choose(GRAPH, length)
  end

  ALPHANUMERIC = [*'A'..'Z', *'a'..'z', *'0'..'9']
  # SecureRandom.alphanumeric generates a random alphanumeric string.
  #
  # The argument _n_ specifies the length of the random number to be
  # generated.
  #
  # The result may contain A-Z, a-z and 0-9.
  #
  #   p SecureRandom.alphanumeric #=> "2BuBuLf3WfSKyQbRccA"
  #   p SecureRandom.alphanumeric #=> "6BbW0pxO0YENxn38HMUbcQ"
  #
  # If a secure random number generator is not available,
  # +NotImplementedError+ is raised.
  def self.alphanumeric(length)
    choose(ALPHANUMERIC, length)
  end

Updated by andrewcbutterfield@gmail.com (Andrew Butterfield) over 4 years ago

This is a fantastic interface and I really like the choose method.

Updated by recursive-madman (Recursive Madman) over 4 years ago

Suggestion: Rename graph to printable and possibly add an :allow_space option?

Updated by kosaki (Motohiro KOSAKI) over 4 years ago

This proposal doesn't contain a use-case nor proposal justification. So, nobody can judge this is good interface or not.

Updated by f3ndot (Justin Bull) almost 2 years ago

As an end user of Ruby, I'd like to be able to generate all sorts of cryptographically secure random characters. There's value in having only alphabetical or alphanumeric chars as they are slightly more human readable.

My use case is offline recovery codes that I expect individuals to write down.

#8

Updated by akr (Akira Tanaka) almost 2 years ago

  • Status changed from Open to Closed

Applied in changeset trunk|r60297.


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.

Also available in: Atom PDF