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