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