Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby18/lib64/ruby/1.8
File: securerandom.rb
# = Secure random number generator interface.
[0] Fix | Delete
#
[1] Fix | Delete
# This library is an interface for secure random number generator which is
[2] Fix | Delete
# suitable for generating session key in HTTP cookies, etc.
[3] Fix | Delete
#
[4] Fix | Delete
# It supports following secure random number generators.
[5] Fix | Delete
#
[6] Fix | Delete
# * openssl
[7] Fix | Delete
# * /dev/urandom
[8] Fix | Delete
#
[9] Fix | Delete
# == Example
[10] Fix | Delete
#
[11] Fix | Delete
# # random hexadecimal string.
[12] Fix | Delete
# p SecureRandom.hex(10) #=> "52750b30ffbc7de3b362"
[13] Fix | Delete
# p SecureRandom.hex(10) #=> "92b15d6c8dc4beb5f559"
[14] Fix | Delete
# p SecureRandom.hex(11) #=> "6aca1b5c58e4863e6b81b8"
[15] Fix | Delete
# p SecureRandom.hex(12) #=> "94b2fff3e7fd9b9c391a2306"
[16] Fix | Delete
# p SecureRandom.hex(13) #=> "39b290146bea6ce975c37cfc23"
[17] Fix | Delete
# ...
[18] Fix | Delete
#
[19] Fix | Delete
# # random base64 string.
[20] Fix | Delete
# p SecureRandom.base64(10) #=> "EcmTPZwWRAozdA=="
[21] Fix | Delete
# p SecureRandom.base64(10) #=> "9b0nsevdwNuM/w=="
[22] Fix | Delete
# p SecureRandom.base64(10) #=> "KO1nIU+p9DKxGg=="
[23] Fix | Delete
# p SecureRandom.base64(11) #=> "l7XEiFja+8EKEtY="
[24] Fix | Delete
# p SecureRandom.base64(12) #=> "7kJSM/MzBJI+75j8"
[25] Fix | Delete
# p SecureRandom.base64(13) #=> "vKLJ0tXBHqQOuIcSIg=="
[26] Fix | Delete
# ...
[27] Fix | Delete
#
[28] Fix | Delete
# # random binary string.
[29] Fix | Delete
# p SecureRandom.random_bytes(10) #=> "\016\t{\370g\310pbr\301"
[30] Fix | Delete
# p SecureRandom.random_bytes(10) #=> "\323U\030TO\234\357\020\a\337"
[31] Fix | Delete
# ...
[32] Fix | Delete
[33] Fix | Delete
begin
[34] Fix | Delete
require 'openssl'
[35] Fix | Delete
rescue LoadError
[36] Fix | Delete
end
[37] Fix | Delete
[38] Fix | Delete
module SecureRandom
[39] Fix | Delete
# SecureRandom.random_bytes generates a random binary string.
[40] Fix | Delete
#
[41] Fix | Delete
# The argument n specifies the length of the result string.
[42] Fix | Delete
#
[43] Fix | Delete
# If n is not specified, 16 is assumed.
[44] Fix | Delete
# It may be larger in future.
[45] Fix | Delete
#
[46] Fix | Delete
# If secure random number generator is not available,
[47] Fix | Delete
# NotImplementedError is raised.
[48] Fix | Delete
def self.random_bytes(n=nil)
[49] Fix | Delete
n ||= 16
[50] Fix | Delete
if defined? OpenSSL::Random
[51] Fix | Delete
@pid = 0 if !defined?(@pid)
[52] Fix | Delete
pid = $$
[53] Fix | Delete
if @pid != pid
[54] Fix | Delete
now = Time.now
[55] Fix | Delete
ary = [now.to_i, now.usec, @pid, pid]
[56] Fix | Delete
OpenSSL::Random.seed(ary.join('.'))
[57] Fix | Delete
@pid = pid
[58] Fix | Delete
end
[59] Fix | Delete
return OpenSSL::Random.random_bytes(n)
[60] Fix | Delete
end
[61] Fix | Delete
if !defined?(@has_urandom) || @has_urandom
[62] Fix | Delete
@has_urandom = false
[63] Fix | Delete
flags = File::RDONLY
[64] Fix | Delete
flags |= File::NONBLOCK if defined? File::NONBLOCK
[65] Fix | Delete
flags |= File::NOCTTY if defined? File::NOCTTY
[66] Fix | Delete
flags |= File::NOFOLLOW if defined? File::NOFOLLOW
[67] Fix | Delete
begin
[68] Fix | Delete
File.open("/dev/urandom", flags) {|f|
[69] Fix | Delete
unless f.stat.chardev?
[70] Fix | Delete
raise Errno::ENOENT
[71] Fix | Delete
end
[72] Fix | Delete
@has_urandom = true
[73] Fix | Delete
ret = f.readpartial(n)
[74] Fix | Delete
if ret.length != n
[75] Fix | Delete
raise NotImplementedError, "Unexpected partial read from random device"
[76] Fix | Delete
end
[77] Fix | Delete
return ret
[78] Fix | Delete
}
[79] Fix | Delete
rescue Errno::ENOENT
[80] Fix | Delete
raise NotImplementedError, "No random device"
[81] Fix | Delete
end
[82] Fix | Delete
end
[83] Fix | Delete
raise NotImplementedError, "No random device"
[84] Fix | Delete
end
[85] Fix | Delete
[86] Fix | Delete
# SecureRandom.hex generates a random hex string.
[87] Fix | Delete
#
[88] Fix | Delete
# The argument n specifies the length of the random length.
[89] Fix | Delete
# The length of the result string is twice of n.
[90] Fix | Delete
#
[91] Fix | Delete
# If n is not specified, 16 is assumed.
[92] Fix | Delete
# It may be larger in future.
[93] Fix | Delete
#
[94] Fix | Delete
# If secure random number generator is not available,
[95] Fix | Delete
# NotImplementedError is raised.
[96] Fix | Delete
def self.hex(n=nil)
[97] Fix | Delete
random_bytes(n).unpack("H*")[0]
[98] Fix | Delete
end
[99] Fix | Delete
[100] Fix | Delete
# SecureRandom.base64 generates a random base64 string.
[101] Fix | Delete
#
[102] Fix | Delete
# The argument n specifies the length of the random length.
[103] Fix | Delete
# The length of the result string is about 4/3 of n.
[104] Fix | Delete
#
[105] Fix | Delete
# If n is not specified, 16 is assumed.
[106] Fix | Delete
# It may be larger in future.
[107] Fix | Delete
#
[108] Fix | Delete
# If secure random number generator is not available,
[109] Fix | Delete
# NotImplementedError is raised.
[110] Fix | Delete
def self.base64(n=nil)
[111] Fix | Delete
[random_bytes(n)].pack("m*").delete("\n")
[112] Fix | Delete
end
[113] Fix | Delete
[114] Fix | Delete
# SecureRandom.random_number generates a random number.
[115] Fix | Delete
#
[116] Fix | Delete
# If an positive integer is given as n,
[117] Fix | Delete
# SecureRandom.random_number returns an integer:
[118] Fix | Delete
# 0 <= SecureRandom.random_number(n) < n.
[119] Fix | Delete
#
[120] Fix | Delete
# If 0 is given or an argument is not given,
[121] Fix | Delete
# SecureRandom.random_number returns an float:
[122] Fix | Delete
# 0.0 <= SecureRandom.random_number() < 1.0.
[123] Fix | Delete
def self.random_number(n=0)
[124] Fix | Delete
if 0 < n
[125] Fix | Delete
hex = n.to_s(16)
[126] Fix | Delete
hex = '0' + hex if (hex.length & 1) == 1
[127] Fix | Delete
bin = [hex].pack("H*")
[128] Fix | Delete
mask = bin[0]
[129] Fix | Delete
mask |= mask >> 1
[130] Fix | Delete
mask |= mask >> 2
[131] Fix | Delete
mask |= mask >> 4
[132] Fix | Delete
begin
[133] Fix | Delete
rnd = SecureRandom.random_bytes(bin.length)
[134] Fix | Delete
rnd[0] = (rnd[0] & mask).chr
[135] Fix | Delete
end until rnd < bin
[136] Fix | Delete
rnd.unpack("H*")[0].hex
[137] Fix | Delete
else
[138] Fix | Delete
# assumption: Float::MANT_DIG <= 64
[139] Fix | Delete
i64 = SecureRandom.random_bytes(8).unpack("Q")[0]
[140] Fix | Delete
Math.ldexp(i64 >> (64-Float::MANT_DIG), -Float::MANT_DIG)
[141] Fix | Delete
end
[142] Fix | Delete
end
[143] Fix | Delete
end
[144] Fix | Delete
[145] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function