2014-11-09 04:31:05 +03:00
|
|
|
# -*- coding: us-ascii -*-
|
2015-11-10 14:48:14 +03:00
|
|
|
# frozen_string_literal: true
|
2013-05-20 15:37:04 +04:00
|
|
|
|
|
|
|
# == Secure random number generator interface.
|
2007-06-10 05:42:51 +04:00
|
|
|
#
|
2015-01-02 09:36:00 +03:00
|
|
|
# This library is an interface to secure random number generators which are
|
|
|
|
# suitable for generating session keys in HTTP cookies, etc.
|
2007-06-10 05:42:51 +04:00
|
|
|
#
|
2014-02-01 01:12:49 +04:00
|
|
|
# You can use this library in your application by requiring it:
|
|
|
|
#
|
|
|
|
# require 'securerandom'
|
|
|
|
#
|
2015-01-02 09:36:00 +03:00
|
|
|
# It supports the following secure random number generators:
|
2007-06-10 05:42:51 +04:00
|
|
|
#
|
|
|
|
# * openssl
|
|
|
|
# * /dev/urandom
|
2008-01-06 12:11:34 +03:00
|
|
|
# * Win32
|
2007-06-10 05:42:51 +04:00
|
|
|
#
|
2019-01-20 18:06:11 +03:00
|
|
|
# SecureRandom is extended by the Random::Formatter module which
|
|
|
|
# defines the following methods:
|
2018-12-27 15:42:45 +03:00
|
|
|
#
|
|
|
|
# * alphanumeric
|
|
|
|
# * base64
|
|
|
|
# * choose
|
|
|
|
# * gen_random
|
|
|
|
# * hex
|
|
|
|
# * rand
|
|
|
|
# * random_bytes
|
|
|
|
# * random_number
|
|
|
|
# * urlsafe_base64
|
|
|
|
# * uuid
|
|
|
|
#
|
|
|
|
# These methods are usable as class methods of SecureRandom such as
|
|
|
|
# `SecureRandom.hex`.
|
|
|
|
#
|
2013-05-20 15:37:04 +04:00
|
|
|
# === Examples
|
|
|
|
#
|
2015-01-02 09:36:00 +03:00
|
|
|
# Generate random hexadecimal strings:
|
2013-05-20 15:37:04 +04:00
|
|
|
#
|
2014-02-01 01:12:49 +04:00
|
|
|
# require 'securerandom'
|
|
|
|
#
|
2018-05-11 23:07:32 +03:00
|
|
|
# SecureRandom.hex(10) #=> "52750b30ffbc7de3b362"
|
|
|
|
# SecureRandom.hex(10) #=> "92b15d6c8dc4beb5f559"
|
|
|
|
# SecureRandom.hex(13) #=> "39b290146bea6ce975c37cfc23"
|
2013-05-20 15:37:04 +04:00
|
|
|
#
|
2015-01-02 09:36:00 +03:00
|
|
|
# Generate random base64 strings:
|
2013-05-20 15:37:04 +04:00
|
|
|
#
|
2018-05-11 23:07:32 +03:00
|
|
|
# SecureRandom.base64(10) #=> "EcmTPZwWRAozdA=="
|
|
|
|
# SecureRandom.base64(10) #=> "KO1nIU+p9DKxGg=="
|
|
|
|
# SecureRandom.base64(12) #=> "7kJSM/MzBJI+75j8"
|
2013-05-20 15:37:04 +04:00
|
|
|
#
|
2015-01-02 09:36:00 +03:00
|
|
|
# Generate random binary strings:
|
2013-05-20 15:37:04 +04:00
|
|
|
#
|
2018-05-11 23:07:32 +03:00
|
|
|
# SecureRandom.random_bytes(10) #=> "\016\t{\370g\310pbr\301"
|
|
|
|
# SecureRandom.random_bytes(10) #=> "\323U\030TO\234\357\020\a\337"
|
2015-01-02 09:36:00 +03:00
|
|
|
#
|
2018-06-12 23:22:43 +03:00
|
|
|
# Generate alphanumeric strings:
|
|
|
|
#
|
|
|
|
# SecureRandom.alphanumeric(10) #=> "S8baxMJnPl"
|
|
|
|
# SecureRandom.alphanumeric(10) #=> "aOxAg8BAJe"
|
|
|
|
#
|
2015-01-02 09:36:00 +03:00
|
|
|
# Generate UUIDs:
|
|
|
|
#
|
2018-05-11 23:07:32 +03:00
|
|
|
# SecureRandom.uuid #=> "2d931510-d99f-494a-8c67-87feb05e1594"
|
|
|
|
# SecureRandom.uuid #=> "bad85eb9-0713-4da7-8d36-07a8e4b00eab"
|
2015-01-02 09:36:00 +03:00
|
|
|
#
|
|
|
|
|
2007-06-10 05:42:51 +04:00
|
|
|
module SecureRandom
|
2017-01-20 11:00:00 +03:00
|
|
|
@rng_chooser = Mutex.new # :nodoc:
|
|
|
|
|
|
|
|
class << self
|
|
|
|
def bytes(n)
|
|
|
|
return gen_random(n)
|
|
|
|
end
|
|
|
|
|
|
|
|
def gen_random(n)
|
2017-09-11 15:44:51 +03:00
|
|
|
ret = Random.urandom(1)
|
2017-01-20 11:00:00 +03:00
|
|
|
if ret.nil?
|
|
|
|
begin
|
|
|
|
require 'openssl'
|
|
|
|
rescue NoMethodError
|
|
|
|
raise NotImplementedError, "No random device"
|
|
|
|
else
|
|
|
|
@rng_chooser.synchronize do
|
|
|
|
class << self
|
|
|
|
remove_method :gen_random
|
|
|
|
alias gen_random gen_random_openssl
|
2019-05-14 05:44:20 +03:00
|
|
|
public :gen_random
|
2017-01-20 11:00:00 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return gen_random(n)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@rng_chooser.synchronize do
|
|
|
|
class << self
|
|
|
|
remove_method :gen_random
|
|
|
|
alias gen_random gen_random_urandom
|
2019-05-14 05:44:20 +03:00
|
|
|
public :gen_random
|
2017-01-20 11:00:00 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return gen_random(n)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def gen_random_openssl(n)
|
2013-07-11 08:01:47 +04:00
|
|
|
@pid = 0 unless defined?(@pid)
|
2011-06-13 13:36:48 +04:00
|
|
|
pid = $$
|
2013-07-11 08:01:47 +04:00
|
|
|
unless @pid == pid
|
2013-08-31 09:07:56 +04:00
|
|
|
now = Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond)
|
2016-03-04 16:39:46 +03:00
|
|
|
OpenSSL::Random.random_add([now, @pid, pid].join(""), 0.0)
|
2017-02-24 14:33:06 +03:00
|
|
|
seed = Random.urandom(16)
|
2015-11-30 23:29:22 +03:00
|
|
|
if (seed)
|
|
|
|
OpenSSL::Random.random_add(seed, 16)
|
|
|
|
end
|
2011-06-13 13:36:48 +04:00
|
|
|
@pid = pid
|
|
|
|
end
|
2007-06-10 05:42:51 +04:00
|
|
|
return OpenSSL::Random.random_bytes(n)
|
|
|
|
end
|
2017-01-20 11:00:00 +03:00
|
|
|
|
|
|
|
def gen_random_urandom(n)
|
|
|
|
ret = Random.urandom(n)
|
2015-02-14 06:01:36 +03:00
|
|
|
unless ret
|
|
|
|
raise NotImplementedError, "No random device"
|
2008-01-06 12:11:34 +03:00
|
|
|
end
|
2015-02-14 06:01:36 +03:00
|
|
|
unless ret.length == n
|
|
|
|
raise NotImplementedError, "Unexpected partial read from random device: only #{ret.length} for #{n} bytes"
|
|
|
|
end
|
|
|
|
ret
|
2014-11-09 06:16:24 +03:00
|
|
|
end
|
2007-06-10 05:42:51 +04:00
|
|
|
end
|
2015-02-14 06:02:32 +03:00
|
|
|
end
|
2007-06-10 05:42:51 +04:00
|
|
|
|
2015-02-14 06:02:32 +03:00
|
|
|
module Random::Formatter
|
2015-02-14 06:20:04 +03:00
|
|
|
|
|
|
|
# SecureRandom.random_bytes generates a random binary string.
|
|
|
|
#
|
|
|
|
# The argument _n_ specifies the length of the result string.
|
|
|
|
#
|
|
|
|
# If _n_ is not specified or is nil, 16 is assumed.
|
|
|
|
# It may be larger in future.
|
|
|
|
#
|
|
|
|
# The result may contain any byte: "\x00" - "\xff".
|
|
|
|
#
|
2018-05-05 06:12:20 +03:00
|
|
|
# require 'securerandom'
|
|
|
|
#
|
2018-05-11 23:07:32 +03:00
|
|
|
# SecureRandom.random_bytes #=> "\xD8\\\xE0\xF4\r\xB2\xFC*WM\xFF\x83\x18\xF45\xB6"
|
|
|
|
# SecureRandom.random_bytes #=> "m\xDC\xFC/\a\x00Uf\xB2\xB2P\xBD\xFF6S\x97"
|
2015-02-14 06:20:04 +03:00
|
|
|
#
|
|
|
|
# If a secure random number generator is not available,
|
|
|
|
# +NotImplementedError+ is raised.
|
|
|
|
def random_bytes(n=nil)
|
|
|
|
n = n ? n.to_int : 16
|
|
|
|
gen_random(n)
|
|
|
|
end
|
|
|
|
|
2013-09-20 18:40:08 +04:00
|
|
|
# SecureRandom.hex generates a random hexadecimal string.
|
2007-06-10 05:42:51 +04:00
|
|
|
#
|
2013-09-20 18:40:08 +04:00
|
|
|
# The argument _n_ specifies the length, in bytes, of the random number to be generated.
|
2015-01-02 09:36:00 +03:00
|
|
|
# The length of the resulting hexadecimal string is twice of _n_.
|
2007-06-10 05:42:51 +04:00
|
|
|
#
|
2013-11-07 22:04:05 +04:00
|
|
|
# If _n_ is not specified or is nil, 16 is assumed.
|
2015-01-02 09:36:00 +03:00
|
|
|
# It may be larger in the future.
|
2007-06-10 06:47:41 +04:00
|
|
|
#
|
2009-02-15 10:58:31 +03:00
|
|
|
# The result may contain 0-9 and a-f.
|
|
|
|
#
|
2018-05-05 06:12:20 +03:00
|
|
|
# require 'securerandom'
|
|
|
|
#
|
2018-05-11 23:07:32 +03:00
|
|
|
# SecureRandom.hex #=> "eb693ec8252cd630102fd0d0fb7c3485"
|
|
|
|
# SecureRandom.hex #=> "91dc3bfb4de5b11d029d376634589b61"
|
2009-02-15 10:58:31 +03:00
|
|
|
#
|
2015-01-02 09:36:00 +03:00
|
|
|
# If a secure random number generator is not available,
|
|
|
|
# +NotImplementedError+ is raised.
|
2015-02-14 06:02:32 +03:00
|
|
|
def hex(n=nil)
|
2007-06-10 05:42:51 +04:00
|
|
|
random_bytes(n).unpack("H*")[0]
|
|
|
|
end
|
|
|
|
|
|
|
|
# SecureRandom.base64 generates a random base64 string.
|
|
|
|
#
|
2013-11-07 22:04:05 +04:00
|
|
|
# The argument _n_ specifies the length, in bytes, of the random number
|
|
|
|
# to be generated. The length of the result string is about 4/3 of _n_.
|
2007-06-10 05:42:51 +04:00
|
|
|
#
|
2013-11-07 22:04:05 +04:00
|
|
|
# If _n_ is not specified or is nil, 16 is assumed.
|
2015-01-02 09:36:00 +03:00
|
|
|
# It may be larger in the future.
|
2007-06-10 06:47:41 +04:00
|
|
|
#
|
2009-02-15 10:58:31 +03:00
|
|
|
# The result may contain A-Z, a-z, 0-9, "+", "/" and "=".
|
|
|
|
#
|
2018-05-05 06:12:20 +03:00
|
|
|
# require 'securerandom'
|
|
|
|
#
|
2018-05-11 23:07:32 +03:00
|
|
|
# SecureRandom.base64 #=> "/2BuBuLf3+WfSKyQbRcc/A=="
|
|
|
|
# SecureRandom.base64 #=> "6BbW0pxO0YENxn38HMUbcQ=="
|
2009-02-15 10:58:31 +03:00
|
|
|
#
|
2015-01-02 09:36:00 +03:00
|
|
|
# If a secure random number generator is not available,
|
|
|
|
# +NotImplementedError+ is raised.
|
2009-02-15 10:58:31 +03:00
|
|
|
#
|
2010-09-11 23:55:08 +04:00
|
|
|
# See RFC 3548 for the definition of base64.
|
2015-02-14 06:02:32 +03:00
|
|
|
def base64(n=nil)
|
2016-01-10 03:35:43 +03:00
|
|
|
[random_bytes(n)].pack("m0")
|
2007-06-10 05:42:51 +04:00
|
|
|
end
|
|
|
|
|
2009-02-15 10:58:31 +03:00
|
|
|
# SecureRandom.urlsafe_base64 generates a random URL-safe base64 string.
|
|
|
|
#
|
2013-11-07 22:04:05 +04:00
|
|
|
# The argument _n_ specifies the length, in bytes, of the random number
|
|
|
|
# to be generated. The length of the result string is about 4/3 of _n_.
|
2009-02-15 10:58:31 +03:00
|
|
|
#
|
2013-11-07 22:04:05 +04:00
|
|
|
# If _n_ is not specified or is nil, 16 is assumed.
|
2015-01-02 09:36:00 +03:00
|
|
|
# It may be larger in the future.
|
2009-02-15 10:58:31 +03:00
|
|
|
#
|
2009-03-06 17:36:33 +03:00
|
|
|
# The boolean argument _padding_ specifies the padding.
|
|
|
|
# If it is false or nil, padding is not generated.
|
|
|
|
# Otherwise padding is generated.
|
|
|
|
# By default, padding is not generated because "=" may be used as a URL delimiter.
|
2009-02-15 10:58:31 +03:00
|
|
|
#
|
2009-03-02 19:56:40 +03:00
|
|
|
# The result may contain A-Z, a-z, 0-9, "-" and "_".
|
2009-03-06 17:36:33 +03:00
|
|
|
# "=" is also used if _padding_ is true.
|
2009-03-02 19:56:40 +03:00
|
|
|
#
|
2018-05-05 06:12:20 +03:00
|
|
|
# require 'securerandom'
|
|
|
|
#
|
2018-05-11 23:07:32 +03:00
|
|
|
# SecureRandom.urlsafe_base64 #=> "b4GOKm4pOYU_-BOXcrUGDg"
|
|
|
|
# SecureRandom.urlsafe_base64 #=> "UZLdOkzop70Ddx-IJR0ABg"
|
2009-02-15 10:58:31 +03:00
|
|
|
#
|
2018-05-11 23:07:32 +03:00
|
|
|
# SecureRandom.urlsafe_base64(nil, true) #=> "i0XQ-7gglIsHGV2_BNPrdQ=="
|
|
|
|
# SecureRandom.urlsafe_base64(nil, true) #=> "-M8rLhr7JEpJlqFGUMmOxg=="
|
2009-03-06 17:36:33 +03:00
|
|
|
#
|
2015-01-02 09:36:00 +03:00
|
|
|
# If a secure random number generator is not available,
|
|
|
|
# +NotImplementedError+ is raised.
|
2009-02-15 10:58:31 +03:00
|
|
|
#
|
2010-09-11 23:55:08 +04:00
|
|
|
# See RFC 3548 for the definition of URL-safe base64.
|
2015-02-14 06:02:32 +03:00
|
|
|
def urlsafe_base64(n=nil, padding=false)
|
2016-01-10 03:35:43 +03:00
|
|
|
s = [random_bytes(n)].pack("m0")
|
2009-02-15 10:58:31 +03:00
|
|
|
s.tr!("+/", "-_")
|
2013-07-11 08:01:47 +04:00
|
|
|
s.delete!("=") unless padding
|
2009-02-15 10:58:31 +03:00
|
|
|
s
|
|
|
|
end
|
|
|
|
|
2015-01-02 09:36:00 +03:00
|
|
|
# SecureRandom.uuid generates a random v4 UUID (Universally Unique IDentifier).
|
2009-02-15 10:58:31 +03:00
|
|
|
#
|
2018-05-05 06:12:20 +03:00
|
|
|
# require 'securerandom'
|
|
|
|
#
|
2018-05-11 23:07:32 +03:00
|
|
|
# SecureRandom.uuid #=> "2d931510-d99f-494a-8c67-87feb05e1594"
|
|
|
|
# SecureRandom.uuid #=> "bad85eb9-0713-4da7-8d36-07a8e4b00eab"
|
|
|
|
# SecureRandom.uuid #=> "62936e70-1815-439b-bf89-8492855a7e6b"
|
2009-02-15 10:58:31 +03:00
|
|
|
#
|
2009-04-26 19:47:24 +04:00
|
|
|
# The version 4 UUID is purely random (except the version).
|
2015-01-02 09:36:00 +03:00
|
|
|
# It doesn't contain meaningful information such as MAC addresses, timestamps, etc.
|
2009-04-26 19:47:24 +04:00
|
|
|
#
|
2015-11-16 06:34:53 +03:00
|
|
|
# The result contains 122 random bits (15.25 random bytes).
|
|
|
|
#
|
2009-04-26 19:47:24 +04:00
|
|
|
# See RFC 4122 for details of UUID.
|
|
|
|
#
|
2015-02-14 06:02:32 +03:00
|
|
|
def uuid
|
|
|
|
ary = random_bytes(16).unpack("NnnnnN")
|
2009-01-29 07:16:17 +03:00
|
|
|
ary[2] = (ary[2] & 0x0fff) | 0x4000
|
|
|
|
ary[3] = (ary[3] & 0x3fff) | 0x8000
|
2009-01-29 06:20:26 +03:00
|
|
|
"%08x-%04x-%04x-%04x-%04x%08x" % ary
|
|
|
|
end
|
2015-02-14 06:20:04 +03:00
|
|
|
|
2017-10-21 18:21:26 +03:00
|
|
|
private def gen_random(n)
|
2015-02-14 06:20:04 +03:00
|
|
|
self.bytes(n)
|
|
|
|
end
|
2017-10-21 18:21:26 +03:00
|
|
|
|
|
|
|
# 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.
|
|
|
|
#
|
2018-05-05 06:12:20 +03:00
|
|
|
# require 'securerandom'
|
|
|
|
#
|
2018-05-11 23:07:32 +03:00
|
|
|
# SecureRandom.choose([*'l'..'r'], 16) #=> "lmrqpoonmmlqlron"
|
|
|
|
# SecureRandom.choose([*'0'..'9'], 5) #=> "27309"
|
2017-10-21 18:21:26 +03:00
|
|
|
#
|
|
|
|
# If a secure random number generator is not available,
|
|
|
|
# +NotImplementedError+ is raised.
|
|
|
|
private def choose(source, n)
|
|
|
|
size = source.size
|
2017-10-21 19:12:46 +03:00
|
|
|
m = 1
|
|
|
|
limit = size
|
|
|
|
while limit * size <= 0x100000000
|
|
|
|
limit *= size
|
|
|
|
m += 1
|
|
|
|
end
|
|
|
|
result = ''.dup
|
|
|
|
while m <= n
|
|
|
|
rs = random_number(limit)
|
|
|
|
is = rs.digits(size)
|
|
|
|
(m-is.length).times { is << 0 }
|
|
|
|
result << source.values_at(*is).join('')
|
|
|
|
n -= m
|
|
|
|
end
|
|
|
|
if 0 < n
|
|
|
|
rs = random_number(limit)
|
|
|
|
is = rs.digits(size)
|
|
|
|
if is.length < n
|
|
|
|
(n-is.length).times { is << 0 }
|
|
|
|
else
|
|
|
|
is.pop while n < is.length
|
|
|
|
end
|
|
|
|
result.concat source.values_at(*is).join('')
|
|
|
|
end
|
|
|
|
result
|
2017-10-21 18:21:26 +03:00
|
|
|
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.
|
|
|
|
#
|
2018-05-05 06:12:20 +03:00
|
|
|
# require 'securerandom'
|
|
|
|
#
|
2018-05-11 23:07:32 +03:00
|
|
|
# SecureRandom.alphanumeric #=> "2BuBuLf3WfSKyQbR"
|
|
|
|
# SecureRandom.alphanumeric(10) #=> "i6K93NdqiH"
|
2017-10-21 18:21:26 +03:00
|
|
|
#
|
|
|
|
# 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
|
2007-06-10 05:42:51 +04:00
|
|
|
end
|
2015-02-14 06:02:32 +03:00
|
|
|
|
|
|
|
SecureRandom.extend(Random::Formatter)
|