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