2014-11-09 04:31:05 +03:00
|
|
|
# -*- coding: us-ascii -*-
|
2015-10-18 06:58:34 +03:00
|
|
|
# -*- frozen_string_literal: true -*-
|
2013-05-20 15:37:04 +04:00
|
|
|
begin
|
|
|
|
require 'openssl'
|
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
|
|
|
|
# == 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
|
|
|
#
|
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'
|
|
|
|
#
|
2013-05-20 15:37:04 +04:00
|
|
|
# p SecureRandom.hex(10) #=> "52750b30ffbc7de3b362"
|
|
|
|
# p SecureRandom.hex(10) #=> "92b15d6c8dc4beb5f559"
|
|
|
|
# p SecureRandom.hex(13) #=> "39b290146bea6ce975c37cfc23"
|
|
|
|
#
|
2015-01-02 09:36:00 +03:00
|
|
|
# Generate random base64 strings:
|
2013-05-20 15: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 09:36:00 +03:00
|
|
|
# Generate random binary strings:
|
2013-05-20 15: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 09:36:00 +03:00
|
|
|
#
|
|
|
|
# Generate UUIDs:
|
|
|
|
#
|
|
|
|
# p SecureRandom.uuid #=> "2d931510-d99f-494a-8c67-87feb05e1594"
|
|
|
|
# p SecureRandom.uuid #=> "bad85eb9-0713-4da7-8d36-07a8e4b00eab"
|
|
|
|
#
|
|
|
|
|
2007-06-10 05:42:51 +04:00
|
|
|
module SecureRandom
|
2014-11-09 06:16:24 +03:00
|
|
|
if defined? OpenSSL::Random
|
|
|
|
def self.gen_random(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)
|
|
|
|
ary = [now, @pid, pid]
|
2013-04-02 19:09:36 +04:00
|
|
|
OpenSSL::Random.random_add(ary.join("").to_s, 0.0)
|
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
|
2014-11-09 06:16:24 +03:00
|
|
|
else
|
|
|
|
def self.gen_random(n)
|
2015-02-14 06:01:36 +03:00
|
|
|
ret = Random.raw_seed(n)
|
|
|
|
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".
|
|
|
|
#
|
|
|
|
# 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 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.
|
|
|
|
#
|
|
|
|
# p SecureRandom.hex #=> "eb693ec8252cd630102fd0d0fb7c3485"
|
|
|
|
# p SecureRandom.hex #=> "91dc3bfb4de5b11d029d376634589b61"
|
|
|
|
#
|
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 "=".
|
|
|
|
#
|
|
|
|
# p SecureRandom.base64 #=> "/2BuBuLf3+WfSKyQbRcc/A=="
|
|
|
|
# p SecureRandom.base64 #=> "6BbW0pxO0YENxn38HMUbcQ=="
|
|
|
|
#
|
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)
|
2007-06-10 05:42:51 +04:00
|
|
|
[random_bytes(n)].pack("m*").delete("\n")
|
|
|
|
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
|
|
|
#
|
|
|
|
# p SecureRandom.urlsafe_base64 #=> "b4GOKm4pOYU_-BOXcrUGDg"
|
|
|
|
# p SecureRandom.urlsafe_base64 #=> "UZLdOkzop70Ddx-IJR0ABg"
|
2009-02-15 10:58:31 +03:00
|
|
|
#
|
2009-03-06 17:36:33 +03:00
|
|
|
# p SecureRandom.urlsafe_base64(nil, true) #=> "i0XQ-7gglIsHGV2_BNPrdQ=="
|
|
|
|
# p SecureRandom.urlsafe_base64(nil, true) #=> "-M8rLhr7JEpJlqFGUMmOxg=="
|
|
|
|
#
|
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)
|
2009-02-15 10:58:31 +03:00
|
|
|
s = [random_bytes(n)].pack("m*")
|
|
|
|
s.delete!("\n")
|
|
|
|
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-02-14 06:20:04 +03:00
|
|
|
=begin
|
2007-06-10 05:42:51 +04:00
|
|
|
# SecureRandom.random_number generates a random number.
|
|
|
|
#
|
2010-09-11 23:55:08 +04:00
|
|
|
# If a positive integer is given as _n_,
|
2015-01-02 09:36:00 +03:00
|
|
|
# +SecureRandom.random_number+ returns an integer, such that:
|
|
|
|
# +0 <= SecureRandom.random_number(n) < n+.
|
2007-06-10 05:42:51 +04:00
|
|
|
#
|
2009-02-15 10:58:31 +03:00
|
|
|
# p SecureRandom.random_number(100) #=> 15
|
|
|
|
# p SecureRandom.random_number(100) #=> 88
|
|
|
|
#
|
2007-06-10 05:42:51 +04:00
|
|
|
# If 0 is given or an argument is not given,
|
2015-01-02 09:36:00 +03:00
|
|
|
# +SecureRandom.random_number+ returns a float, such that:
|
|
|
|
# +0.0 <= SecureRandom.random_number() < 1.0+.
|
2009-02-15 10:58:31 +03:00
|
|
|
#
|
|
|
|
# p SecureRandom.random_number #=> 0.596506046187744
|
|
|
|
# p SecureRandom.random_number #=> 0.350621695741409
|
|
|
|
#
|
2015-02-14 06:02:32 +03:00
|
|
|
def random_number(n=0)
|
2007-06-10 05:42:51 +04:00
|
|
|
if 0 < n
|
2014-08-08 15: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-14 06:02:32 +03:00
|
|
|
rnd = random_bytes(bin.length)
|
2014-08-08 15:00:47 +04:00
|
|
|
rnd[0] = (rnd[0].ord & mask).chr
|
|
|
|
end until rnd < bin
|
|
|
|
rnd.unpack("H*")[0].hex
|
|
|
|
end
|
2007-06-10 05:42:51 +04:00
|
|
|
else
|
|
|
|
# assumption: Float::MANT_DIG <= 64
|
2014-08-08 15:00:47 +04:00
|
|
|
if defined? OpenSSL::BN
|
|
|
|
i64 = OpenSSL::BN.rand(64, -1).to_i
|
|
|
|
else
|
2015-02-14 06:02:32 +03:00
|
|
|
i64 = random_bytes(8).unpack("Q")[0]
|
2014-08-08 15:00:47 +04:00
|
|
|
end
|
2007-06-10 05:42:51 +04:00
|
|
|
Math.ldexp(i64 >> (64-Float::MANT_DIG), -Float::MANT_DIG)
|
|
|
|
end
|
|
|
|
end
|
2015-02-14 06:20:04 +03:00
|
|
|
=end
|
2008-01-06 12:11:34 +03:00
|
|
|
|
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
|
|
|
#
|
|
|
|
# p SecureRandom.uuid #=> "2d931510-d99f-494a-8c67-87feb05e1594"
|
2009-08-20 00:21:19 +04:00
|
|
|
# p SecureRandom.uuid #=> "bad85eb9-0713-4da7-8d36-07a8e4b00eab"
|
2009-02-15 10:58:31 +03:00
|
|
|
# p SecureRandom.uuid #=> "62936e70-1815-439b-bf89-8492855a7e6b"
|
|
|
|
#
|
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
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
|
|
private
|
|
|
|
def gen_random(n)
|
|
|
|
self.bytes(n)
|
|
|
|
end
|
2007-06-10 05:42:51 +04:00
|
|
|
end
|
2015-02-14 06:02:32 +03:00
|
|
|
|
|
|
|
SecureRandom.extend(Random::Formatter)
|