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
|
|
|
|
2021-12-05 15:53:35 +03:00
|
|
|
require 'random/formatter'
|
|
|
|
|
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
|
2021-08-24 13:24:58 +03:00
|
|
|
# +SecureRandom.hex+.
|
2018-12-27 15:42:45 +03:00
|
|
|
#
|
2021-12-05 15:53:35 +03:00
|
|
|
# If a secure random number generator is not available,
|
|
|
|
# +NotImplementedError+ is raised.
|
2015-01-02 09:36:00 +03:00
|
|
|
|
2007-06-10 05:42:51 +04:00
|
|
|
module SecureRandom
|
2023-03-30 08:07:22 +03:00
|
|
|
|
2023-12-12 08:11:51 +03:00
|
|
|
# The version
|
2023-12-16 07:09:34 +03:00
|
|
|
VERSION = "0.3.1"
|
2023-03-30 08:07:22 +03:00
|
|
|
|
2017-01-20 11:00:00 +03:00
|
|
|
class << self
|
2023-12-12 08:11:51 +03:00
|
|
|
# Returns a random binary string containing +size+ bytes.
|
|
|
|
#
|
|
|
|
# See Random.bytes
|
2017-01-20 11:00:00 +03:00
|
|
|
def bytes(n)
|
|
|
|
return gen_random(n)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-12-12 08:41:09 +03:00
|
|
|
# :stopdoc:
|
|
|
|
|
2023-12-12 08:11:51 +03:00
|
|
|
# Implementation using OpenSSL
|
2017-01-20 11:00:00 +03:00
|
|
|
def gen_random_openssl(n)
|
2007-06-10 05:42:51 +04:00
|
|
|
return OpenSSL::Random.random_bytes(n)
|
|
|
|
end
|
2017-01-20 11:00:00 +03:00
|
|
|
|
2023-12-12 08:11:51 +03:00
|
|
|
# Implementation using system random device
|
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
|
2020-09-05 03:32:31 +03:00
|
|
|
|
2022-02-16 08:15:11 +03:00
|
|
|
begin
|
|
|
|
# Check if Random.urandom is available
|
|
|
|
Random.urandom(1)
|
|
|
|
alias gen_random gen_random_urandom
|
|
|
|
rescue RuntimeError
|
2020-09-05 03:32:31 +03:00
|
|
|
begin
|
|
|
|
require 'openssl'
|
|
|
|
rescue NoMethodError
|
|
|
|
raise NotImplementedError, "No random device"
|
|
|
|
else
|
|
|
|
alias gen_random gen_random_openssl
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-12-12 08:41:09 +03:00
|
|
|
# :startdoc:
|
|
|
|
|
2023-12-12 08:11:51 +03:00
|
|
|
# Generate random data bytes for Random::Formatter
|
2020-09-05 03:32:31 +03:00
|
|
|
public :gen_random
|
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
|
|
|
SecureRandom.extend(Random::Formatter)
|