[ruby-core:68098] [Ruby trunk - Feature #10849] [Open] Adding an alphanumeric function to SecureRandom

0 views
Skip to first unread message

andrewcbu...@gmail.com

unread,
Feb 12, 2015, 6:55:58 PM2/12/15
to ruby...@ruby-lang.org
Issue #10849 has been reported by Andrew Butterfield.

----------------------------------------
Feature #10849: Adding an alphanumeric function to SecureRandom
https://bugs.ruby-lang.org/issues/10849

* Author: Andrew Butterfield
* Status: Open
* Priority: Normal
* Assignee: Andrew Butterfield
----------------------------------------
It would be handy to have a method that produces a random alphanumeric string.



--
https://bugs.ruby-lang.org/

andrewcbu...@gmail.com

unread,
Feb 12, 2015, 6:56:47 PM2/12/15
to ruby...@ruby-lang.org
Issue #10849 has been updated by Andrew Butterfield.

File securerandom.txt added

----------------------------------------
Feature #10849: Adding an alphanumeric function to SecureRandom
https://bugs.ruby-lang.org/issues/10849#change-51477

* Author: Andrew Butterfield
* Status: Open
* Priority: Normal
* Assignee: Andrew Butterfield
----------------------------------------
It would be handy to have a method that produces a random alphanumeric string.

---Files--------------------------------
securerandom.txt (1.32 KB)


--
https://bugs.ruby-lang.org/

andrewcbu...@gmail.com

unread,
Feb 12, 2015, 7:03:00 PM2/12/15
to ruby...@ruby-lang.org
Issue #10849 has been updated by Andrew Butterfield.

File securerandom.patch added

----------------------------------------
Feature #10849: Adding an alphanumeric function to SecureRandom
https://bugs.ruby-lang.org/issues/10849#change-51479

* Author: Andrew Butterfield
* Status: Open
* Priority: Normal
* Assignee: Andrew Butterfield
----------------------------------------
It would be handy to have a method that produces a random alphanumeric string.

---Files--------------------------------
securerandom.txt (1.32 KB)
securerandom.patch (1.32 KB)


--
https://bugs.ruby-lang.org/

no...@ruby-lang.org

unread,
Feb 12, 2015, 9:41:53 PM2/12/15
to ruby...@ruby-lang.org
Issue #10849 has been updated by Nobuyoshi Nakada.


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

I don't like this interface.

~~~ruby
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
~~~

----------------------------------------
Feature #10849: Adding an alphanumeric function to SecureRandom
https://bugs.ruby-lang.org/issues/10849#change-51482
Reply all
Reply to author
Forward
0 new messages