Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../opt/alt/ruby27/share/ruby
File: securerandom.rb
# -*- coding: us-ascii -*-
[0] Fix | Delete
# frozen_string_literal: true
[1] Fix | Delete
[2] Fix | Delete
# == Secure random number generator interface.
[3] Fix | Delete
#
[4] Fix | Delete
# This library is an interface to secure random number generators which are
[5] Fix | Delete
# suitable for generating session keys in HTTP cookies, etc.
[6] Fix | Delete
#
[7] Fix | Delete
# You can use this library in your application by requiring it:
[8] Fix | Delete
#
[9] Fix | Delete
# require 'securerandom'
[10] Fix | Delete
#
[11] Fix | Delete
# It supports the following secure random number generators:
[12] Fix | Delete
#
[13] Fix | Delete
# * openssl
[14] Fix | Delete
# * /dev/urandom
[15] Fix | Delete
# * Win32
[16] Fix | Delete
#
[17] Fix | Delete
# SecureRandom is extended by the Random::Formatter module which
[18] Fix | Delete
# defines the following methods:
[19] Fix | Delete
#
[20] Fix | Delete
# * alphanumeric
[21] Fix | Delete
# * base64
[22] Fix | Delete
# * choose
[23] Fix | Delete
# * gen_random
[24] Fix | Delete
# * hex
[25] Fix | Delete
# * rand
[26] Fix | Delete
# * random_bytes
[27] Fix | Delete
# * random_number
[28] Fix | Delete
# * urlsafe_base64
[29] Fix | Delete
# * uuid
[30] Fix | Delete
#
[31] Fix | Delete
# These methods are usable as class methods of SecureRandom such as
[32] Fix | Delete
# `SecureRandom.hex`.
[33] Fix | Delete
#
[34] Fix | Delete
# === Examples
[35] Fix | Delete
#
[36] Fix | Delete
# Generate random hexadecimal strings:
[37] Fix | Delete
#
[38] Fix | Delete
# require 'securerandom'
[39] Fix | Delete
#
[40] Fix | Delete
# SecureRandom.hex(10) #=> "52750b30ffbc7de3b362"
[41] Fix | Delete
# SecureRandom.hex(10) #=> "92b15d6c8dc4beb5f559"
[42] Fix | Delete
# SecureRandom.hex(13) #=> "39b290146bea6ce975c37cfc23"
[43] Fix | Delete
#
[44] Fix | Delete
# Generate random base64 strings:
[45] Fix | Delete
#
[46] Fix | Delete
# SecureRandom.base64(10) #=> "EcmTPZwWRAozdA=="
[47] Fix | Delete
# SecureRandom.base64(10) #=> "KO1nIU+p9DKxGg=="
[48] Fix | Delete
# SecureRandom.base64(12) #=> "7kJSM/MzBJI+75j8"
[49] Fix | Delete
#
[50] Fix | Delete
# Generate random binary strings:
[51] Fix | Delete
#
[52] Fix | Delete
# SecureRandom.random_bytes(10) #=> "\016\t{\370g\310pbr\301"
[53] Fix | Delete
# SecureRandom.random_bytes(10) #=> "\323U\030TO\234\357\020\a\337"
[54] Fix | Delete
#
[55] Fix | Delete
# Generate alphanumeric strings:
[56] Fix | Delete
#
[57] Fix | Delete
# SecureRandom.alphanumeric(10) #=> "S8baxMJnPl"
[58] Fix | Delete
# SecureRandom.alphanumeric(10) #=> "aOxAg8BAJe"
[59] Fix | Delete
#
[60] Fix | Delete
# Generate UUIDs:
[61] Fix | Delete
#
[62] Fix | Delete
# SecureRandom.uuid #=> "2d931510-d99f-494a-8c67-87feb05e1594"
[63] Fix | Delete
# SecureRandom.uuid #=> "bad85eb9-0713-4da7-8d36-07a8e4b00eab"
[64] Fix | Delete
#
[65] Fix | Delete
[66] Fix | Delete
module SecureRandom
[67] Fix | Delete
@rng_chooser = Mutex.new # :nodoc:
[68] Fix | Delete
[69] Fix | Delete
class << self
[70] Fix | Delete
def bytes(n)
[71] Fix | Delete
return gen_random(n)
[72] Fix | Delete
end
[73] Fix | Delete
[74] Fix | Delete
def gen_random(n)
[75] Fix | Delete
ret = Random.urandom(1)
[76] Fix | Delete
if ret.nil?
[77] Fix | Delete
begin
[78] Fix | Delete
require 'openssl'
[79] Fix | Delete
rescue NoMethodError
[80] Fix | Delete
raise NotImplementedError, "No random device"
[81] Fix | Delete
else
[82] Fix | Delete
@rng_chooser.synchronize do
[83] Fix | Delete
class << self
[84] Fix | Delete
remove_method :gen_random
[85] Fix | Delete
alias gen_random gen_random_openssl
[86] Fix | Delete
public :gen_random
[87] Fix | Delete
end
[88] Fix | Delete
end
[89] Fix | Delete
return gen_random(n)
[90] Fix | Delete
end
[91] Fix | Delete
else
[92] Fix | Delete
@rng_chooser.synchronize do
[93] Fix | Delete
class << self
[94] Fix | Delete
remove_method :gen_random
[95] Fix | Delete
alias gen_random gen_random_urandom
[96] Fix | Delete
public :gen_random
[97] Fix | Delete
end
[98] Fix | Delete
end
[99] Fix | Delete
return gen_random(n)
[100] Fix | Delete
end
[101] Fix | Delete
end
[102] Fix | Delete
[103] Fix | Delete
private
[104] Fix | Delete
[105] Fix | Delete
def gen_random_openssl(n)
[106] Fix | Delete
@pid = 0 unless defined?(@pid)
[107] Fix | Delete
pid = $$
[108] Fix | Delete
unless @pid == pid
[109] Fix | Delete
now = Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond)
[110] Fix | Delete
OpenSSL::Random.random_add([now, @pid, pid].join(""), 0.0)
[111] Fix | Delete
seed = Random.urandom(16)
[112] Fix | Delete
if (seed)
[113] Fix | Delete
OpenSSL::Random.random_add(seed, 16)
[114] Fix | Delete
end
[115] Fix | Delete
@pid = pid
[116] Fix | Delete
end
[117] Fix | Delete
return OpenSSL::Random.random_bytes(n)
[118] Fix | Delete
end
[119] Fix | Delete
[120] Fix | Delete
def gen_random_urandom(n)
[121] Fix | Delete
ret = Random.urandom(n)
[122] Fix | Delete
unless ret
[123] Fix | Delete
raise NotImplementedError, "No random device"
[124] Fix | Delete
end
[125] Fix | Delete
unless ret.length == n
[126] Fix | Delete
raise NotImplementedError, "Unexpected partial read from random device: only #{ret.length} for #{n} bytes"
[127] Fix | Delete
end
[128] Fix | Delete
ret
[129] Fix | Delete
end
[130] Fix | Delete
end
[131] Fix | Delete
end
[132] Fix | Delete
[133] Fix | Delete
module Random::Formatter
[134] Fix | Delete
[135] Fix | Delete
# SecureRandom.random_bytes generates a random binary string.
[136] Fix | Delete
#
[137] Fix | Delete
# The argument _n_ specifies the length of the result string.
[138] Fix | Delete
#
[139] Fix | Delete
# If _n_ is not specified or is nil, 16 is assumed.
[140] Fix | Delete
# It may be larger in future.
[141] Fix | Delete
#
[142] Fix | Delete
# The result may contain any byte: "\x00" - "\xff".
[143] Fix | Delete
#
[144] Fix | Delete
# require 'securerandom'
[145] Fix | Delete
#
[146] Fix | Delete
# SecureRandom.random_bytes #=> "\xD8\\\xE0\xF4\r\xB2\xFC*WM\xFF\x83\x18\xF45\xB6"
[147] Fix | Delete
# SecureRandom.random_bytes #=> "m\xDC\xFC/\a\x00Uf\xB2\xB2P\xBD\xFF6S\x97"
[148] Fix | Delete
#
[149] Fix | Delete
# If a secure random number generator is not available,
[150] Fix | Delete
# +NotImplementedError+ is raised.
[151] Fix | Delete
def random_bytes(n=nil)
[152] Fix | Delete
n = n ? n.to_int : 16
[153] Fix | Delete
gen_random(n)
[154] Fix | Delete
end
[155] Fix | Delete
[156] Fix | Delete
# SecureRandom.hex generates a random hexadecimal string.
[157] Fix | Delete
#
[158] Fix | Delete
# The argument _n_ specifies the length, in bytes, of the random number to be generated.
[159] Fix | Delete
# The length of the resulting hexadecimal string is twice of _n_.
[160] Fix | Delete
#
[161] Fix | Delete
# If _n_ is not specified or is nil, 16 is assumed.
[162] Fix | Delete
# It may be larger in the future.
[163] Fix | Delete
#
[164] Fix | Delete
# The result may contain 0-9 and a-f.
[165] Fix | Delete
#
[166] Fix | Delete
# require 'securerandom'
[167] Fix | Delete
#
[168] Fix | Delete
# SecureRandom.hex #=> "eb693ec8252cd630102fd0d0fb7c3485"
[169] Fix | Delete
# SecureRandom.hex #=> "91dc3bfb4de5b11d029d376634589b61"
[170] Fix | Delete
#
[171] Fix | Delete
# If a secure random number generator is not available,
[172] Fix | Delete
# +NotImplementedError+ is raised.
[173] Fix | Delete
def hex(n=nil)
[174] Fix | Delete
random_bytes(n).unpack("H*")[0]
[175] Fix | Delete
end
[176] Fix | Delete
[177] Fix | Delete
# SecureRandom.base64 generates a random base64 string.
[178] Fix | Delete
#
[179] Fix | Delete
# The argument _n_ specifies the length, in bytes, of the random number
[180] Fix | Delete
# to be generated. The length of the result string is about 4/3 of _n_.
[181] Fix | Delete
#
[182] Fix | Delete
# If _n_ is not specified or is nil, 16 is assumed.
[183] Fix | Delete
# It may be larger in the future.
[184] Fix | Delete
#
[185] Fix | Delete
# The result may contain A-Z, a-z, 0-9, "+", "/" and "=".
[186] Fix | Delete
#
[187] Fix | Delete
# require 'securerandom'
[188] Fix | Delete
#
[189] Fix | Delete
# SecureRandom.base64 #=> "/2BuBuLf3+WfSKyQbRcc/A=="
[190] Fix | Delete
# SecureRandom.base64 #=> "6BbW0pxO0YENxn38HMUbcQ=="
[191] Fix | Delete
#
[192] Fix | Delete
# If a secure random number generator is not available,
[193] Fix | Delete
# +NotImplementedError+ is raised.
[194] Fix | Delete
#
[195] Fix | Delete
# See RFC 3548 for the definition of base64.
[196] Fix | Delete
def base64(n=nil)
[197] Fix | Delete
[random_bytes(n)].pack("m0")
[198] Fix | Delete
end
[199] Fix | Delete
[200] Fix | Delete
# SecureRandom.urlsafe_base64 generates a random URL-safe base64 string.
[201] Fix | Delete
#
[202] Fix | Delete
# The argument _n_ specifies the length, in bytes, of the random number
[203] Fix | Delete
# to be generated. The length of the result string is about 4/3 of _n_.
[204] Fix | Delete
#
[205] Fix | Delete
# If _n_ is not specified or is nil, 16 is assumed.
[206] Fix | Delete
# It may be larger in the future.
[207] Fix | Delete
#
[208] Fix | Delete
# The boolean argument _padding_ specifies the padding.
[209] Fix | Delete
# If it is false or nil, padding is not generated.
[210] Fix | Delete
# Otherwise padding is generated.
[211] Fix | Delete
# By default, padding is not generated because "=" may be used as a URL delimiter.
[212] Fix | Delete
#
[213] Fix | Delete
# The result may contain A-Z, a-z, 0-9, "-" and "_".
[214] Fix | Delete
# "=" is also used if _padding_ is true.
[215] Fix | Delete
#
[216] Fix | Delete
# require 'securerandom'
[217] Fix | Delete
#
[218] Fix | Delete
# SecureRandom.urlsafe_base64 #=> "b4GOKm4pOYU_-BOXcrUGDg"
[219] Fix | Delete
# SecureRandom.urlsafe_base64 #=> "UZLdOkzop70Ddx-IJR0ABg"
[220] Fix | Delete
#
[221] Fix | Delete
# SecureRandom.urlsafe_base64(nil, true) #=> "i0XQ-7gglIsHGV2_BNPrdQ=="
[222] Fix | Delete
# SecureRandom.urlsafe_base64(nil, true) #=> "-M8rLhr7JEpJlqFGUMmOxg=="
[223] Fix | Delete
#
[224] Fix | Delete
# If a secure random number generator is not available,
[225] Fix | Delete
# +NotImplementedError+ is raised.
[226] Fix | Delete
#
[227] Fix | Delete
# See RFC 3548 for the definition of URL-safe base64.
[228] Fix | Delete
def urlsafe_base64(n=nil, padding=false)
[229] Fix | Delete
s = [random_bytes(n)].pack("m0")
[230] Fix | Delete
s.tr!("+/", "-_")
[231] Fix | Delete
s.delete!("=") unless padding
[232] Fix | Delete
s
[233] Fix | Delete
end
[234] Fix | Delete
[235] Fix | Delete
# SecureRandom.uuid generates a random v4 UUID (Universally Unique IDentifier).
[236] Fix | Delete
#
[237] Fix | Delete
# require 'securerandom'
[238] Fix | Delete
#
[239] Fix | Delete
# SecureRandom.uuid #=> "2d931510-d99f-494a-8c67-87feb05e1594"
[240] Fix | Delete
# SecureRandom.uuid #=> "bad85eb9-0713-4da7-8d36-07a8e4b00eab"
[241] Fix | Delete
# SecureRandom.uuid #=> "62936e70-1815-439b-bf89-8492855a7e6b"
[242] Fix | Delete
#
[243] Fix | Delete
# The version 4 UUID is purely random (except the version).
[244] Fix | Delete
# It doesn't contain meaningful information such as MAC addresses, timestamps, etc.
[245] Fix | Delete
#
[246] Fix | Delete
# The result contains 122 random bits (15.25 random bytes).
[247] Fix | Delete
#
[248] Fix | Delete
# See RFC 4122 for details of UUID.
[249] Fix | Delete
#
[250] Fix | Delete
def uuid
[251] Fix | Delete
ary = random_bytes(16).unpack("NnnnnN")
[252] Fix | Delete
ary[2] = (ary[2] & 0x0fff) | 0x4000
[253] Fix | Delete
ary[3] = (ary[3] & 0x3fff) | 0x8000
[254] Fix | Delete
"%08x-%04x-%04x-%04x-%04x%08x" % ary
[255] Fix | Delete
end
[256] Fix | Delete
[257] Fix | Delete
private def gen_random(n)
[258] Fix | Delete
self.bytes(n)
[259] Fix | Delete
end
[260] Fix | Delete
[261] Fix | Delete
# SecureRandom.choose generates a string that randomly draws from a
[262] Fix | Delete
# source array of characters.
[263] Fix | Delete
#
[264] Fix | Delete
# The argument _source_ specifies the array of characters from which
[265] Fix | Delete
# to generate the string.
[266] Fix | Delete
# The argument _n_ specifies the length, in characters, of the string to be
[267] Fix | Delete
# generated.
[268] Fix | Delete
#
[269] Fix | Delete
# The result may contain whatever characters are in the source array.
[270] Fix | Delete
#
[271] Fix | Delete
# require 'securerandom'
[272] Fix | Delete
#
[273] Fix | Delete
# SecureRandom.choose([*'l'..'r'], 16) #=> "lmrqpoonmmlqlron"
[274] Fix | Delete
# SecureRandom.choose([*'0'..'9'], 5) #=> "27309"
[275] Fix | Delete
#
[276] Fix | Delete
# If a secure random number generator is not available,
[277] Fix | Delete
# +NotImplementedError+ is raised.
[278] Fix | Delete
private def choose(source, n)
[279] Fix | Delete
size = source.size
[280] Fix | Delete
m = 1
[281] Fix | Delete
limit = size
[282] Fix | Delete
while limit * size <= 0x100000000
[283] Fix | Delete
limit *= size
[284] Fix | Delete
m += 1
[285] Fix | Delete
end
[286] Fix | Delete
result = ''.dup
[287] Fix | Delete
while m <= n
[288] Fix | Delete
rs = random_number(limit)
[289] Fix | Delete
is = rs.digits(size)
[290] Fix | Delete
(m-is.length).times { is << 0 }
[291] Fix | Delete
result << source.values_at(*is).join('')
[292] Fix | Delete
n -= m
[293] Fix | Delete
end
[294] Fix | Delete
if 0 < n
[295] Fix | Delete
rs = random_number(limit)
[296] Fix | Delete
is = rs.digits(size)
[297] Fix | Delete
if is.length < n
[298] Fix | Delete
(n-is.length).times { is << 0 }
[299] Fix | Delete
else
[300] Fix | Delete
is.pop while n < is.length
[301] Fix | Delete
end
[302] Fix | Delete
result.concat source.values_at(*is).join('')
[303] Fix | Delete
end
[304] Fix | Delete
result
[305] Fix | Delete
end
[306] Fix | Delete
[307] Fix | Delete
ALPHANUMERIC = [*'A'..'Z', *'a'..'z', *'0'..'9']
[308] Fix | Delete
# SecureRandom.alphanumeric generates a random alphanumeric string.
[309] Fix | Delete
#
[310] Fix | Delete
# The argument _n_ specifies the length, in characters, of the alphanumeric
[311] Fix | Delete
# string to be generated.
[312] Fix | Delete
#
[313] Fix | Delete
# If _n_ is not specified or is nil, 16 is assumed.
[314] Fix | Delete
# It may be larger in the future.
[315] Fix | Delete
#
[316] Fix | Delete
# The result may contain A-Z, a-z and 0-9.
[317] Fix | Delete
#
[318] Fix | Delete
# require 'securerandom'
[319] Fix | Delete
#
[320] Fix | Delete
# SecureRandom.alphanumeric #=> "2BuBuLf3WfSKyQbR"
[321] Fix | Delete
# SecureRandom.alphanumeric(10) #=> "i6K93NdqiH"
[322] Fix | Delete
#
[323] Fix | Delete
# If a secure random number generator is not available,
[324] Fix | Delete
# +NotImplementedError+ is raised.
[325] Fix | Delete
def alphanumeric(n=nil)
[326] Fix | Delete
n = 16 if n.nil?
[327] Fix | Delete
choose(ALPHANUMERIC, n)
[328] Fix | Delete
end
[329] Fix | Delete
end
[330] Fix | Delete
[331] Fix | Delete
SecureRandom.extend(Random::Formatter)
[332] Fix | Delete
[333] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function