Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/ruby22/lib64/ruby/2.2.0
File: securerandom.rb
# -*- coding: us-ascii -*-
[0] Fix | Delete
begin
[1] Fix | Delete
require 'openssl'
[2] Fix | Delete
rescue LoadError
[3] Fix | Delete
end
[4] Fix | Delete
[5] Fix | Delete
# == Secure random number generator interface.
[6] Fix | Delete
#
[7] Fix | Delete
# This library is an interface for secure random number generator which is
[8] Fix | Delete
# suitable for generating session key in HTTP cookies, etc.
[9] Fix | Delete
#
[10] Fix | Delete
# You can use this library in your application by requiring it:
[11] Fix | Delete
#
[12] Fix | Delete
# require 'securerandom'
[13] Fix | Delete
#
[14] Fix | Delete
# It supports following secure random number generators.
[15] Fix | Delete
#
[16] Fix | Delete
# * openssl
[17] Fix | Delete
# * /dev/urandom
[18] Fix | Delete
# * Win32
[19] Fix | Delete
#
[20] Fix | Delete
# === Examples
[21] Fix | Delete
#
[22] Fix | Delete
# Hexadecimal string.
[23] Fix | Delete
#
[24] Fix | Delete
# require 'securerandom'
[25] Fix | Delete
#
[26] Fix | Delete
# p SecureRandom.hex(10) #=> "52750b30ffbc7de3b362"
[27] Fix | Delete
# p SecureRandom.hex(10) #=> "92b15d6c8dc4beb5f559"
[28] Fix | Delete
# p SecureRandom.hex(13) #=> "39b290146bea6ce975c37cfc23"
[29] Fix | Delete
#
[30] Fix | Delete
# Base64 string.
[31] Fix | Delete
#
[32] Fix | Delete
# p SecureRandom.base64(10) #=> "EcmTPZwWRAozdA=="
[33] Fix | Delete
# p SecureRandom.base64(10) #=> "KO1nIU+p9DKxGg=="
[34] Fix | Delete
# p SecureRandom.base64(12) #=> "7kJSM/MzBJI+75j8"
[35] Fix | Delete
#
[36] Fix | Delete
# Binary string.
[37] Fix | Delete
#
[38] Fix | Delete
# p SecureRandom.random_bytes(10) #=> "\016\t{\370g\310pbr\301"
[39] Fix | Delete
# p SecureRandom.random_bytes(10) #=> "\323U\030TO\234\357\020\a\337"
[40] Fix | Delete
module SecureRandom
[41] Fix | Delete
if /mswin|mingw/ =~ RUBY_PLATFORM
[42] Fix | Delete
require "fiddle/import"
[43] Fix | Delete
[44] Fix | Delete
module AdvApi32 # :nodoc:
[45] Fix | Delete
extend Fiddle::Importer
[46] Fix | Delete
dlload "advapi32"
[47] Fix | Delete
extern "int CryptAcquireContext(void*, void*, void*, unsigned long, unsigned long)"
[48] Fix | Delete
extern "int CryptGenRandom(void*, unsigned long, void*)"
[49] Fix | Delete
[50] Fix | Delete
def self.get_provider
[51] Fix | Delete
hProvStr = " " * Fiddle::SIZEOF_VOIDP
[52] Fix | Delete
prov_rsa_full = 1
[53] Fix | Delete
crypt_verifycontext = 0xF0000000
[54] Fix | Delete
[55] Fix | Delete
if CryptAcquireContext(hProvStr, nil, nil, prov_rsa_full, crypt_verifycontext) == 0
[56] Fix | Delete
raise SystemCallError, "CryptAcquireContext failed: #{lastWin32ErrorMessage}"
[57] Fix | Delete
end
[58] Fix | Delete
type = Fiddle::SIZEOF_VOIDP == Fiddle::SIZEOF_LONG_LONG ? 'q' : 'l'
[59] Fix | Delete
hProv, = hProvStr.unpack(type)
[60] Fix | Delete
hProv
[61] Fix | Delete
end
[62] Fix | Delete
[63] Fix | Delete
def self.gen_random(n)
[64] Fix | Delete
@hProv ||= get_provider
[65] Fix | Delete
bytes = " ".force_encoding("ASCII-8BIT") * n
[66] Fix | Delete
if CryptGenRandom(@hProv, bytes.size, bytes) == 0
[67] Fix | Delete
raise SystemCallError, "CryptGenRandom failed: #{Kernel32.last_error_message}"
[68] Fix | Delete
end
[69] Fix | Delete
bytes
[70] Fix | Delete
end
[71] Fix | Delete
end
[72] Fix | Delete
[73] Fix | Delete
module Kernel32 # :nodoc:
[74] Fix | Delete
extend Fiddle::Importer
[75] Fix | Delete
dlload "kernel32"
[76] Fix | Delete
extern "unsigned long GetLastError()"
[77] Fix | Delete
extern "unsigned long FormatMessageA(unsigned long, void*, unsigned long, unsigned long, void*, unsigned long, void*)"
[78] Fix | Delete
[79] Fix | Delete
# Following code is based on David Garamond's GUID library for Ruby.
[80] Fix | Delete
def self.last_error_message
[81] Fix | Delete
format_message_ignore_inserts = 0x00000200
[82] Fix | Delete
format_message_from_system = 0x00001000
[83] Fix | Delete
[84] Fix | Delete
code = GetLastError()
[85] Fix | Delete
msg = "\0" * 1024
[86] Fix | Delete
len = FormatMessageA(format_message_ignore_inserts + format_message_from_system, 0, code, 0, msg, 1024, nil)
[87] Fix | Delete
msg[0, len].force_encoding("filesystem").tr("\r", '').chomp
[88] Fix | Delete
end
[89] Fix | Delete
end
[90] Fix | Delete
end
[91] Fix | Delete
[92] Fix | Delete
# SecureRandom.random_bytes generates a random binary string.
[93] Fix | Delete
#
[94] Fix | Delete
# The argument _n_ specifies the length of the result string.
[95] Fix | Delete
#
[96] Fix | Delete
# If _n_ is not specified or is nil, 16 is assumed.
[97] Fix | Delete
# It may be larger in future.
[98] Fix | Delete
#
[99] Fix | Delete
# The result may contain any byte: "\x00" - "\xff".
[100] Fix | Delete
#
[101] Fix | Delete
# p SecureRandom.random_bytes #=> "\xD8\\\xE0\xF4\r\xB2\xFC*WM\xFF\x83\x18\xF45\xB6"
[102] Fix | Delete
# p SecureRandom.random_bytes #=> "m\xDC\xFC/\a\x00Uf\xB2\xB2P\xBD\xFF6S\x97"
[103] Fix | Delete
#
[104] Fix | Delete
# If secure random number generator is not available,
[105] Fix | Delete
# NotImplementedError is raised.
[106] Fix | Delete
def self.random_bytes(n=nil)
[107] Fix | Delete
n = n ? n.to_int : 16
[108] Fix | Delete
gen_random(n)
[109] Fix | Delete
end
[110] Fix | Delete
[111] Fix | Delete
if defined?(OpenSSL::Random) && !defined?(AdvApi32)
[112] Fix | Delete
def self.gen_random(n)
[113] Fix | Delete
@pid = 0 unless defined?(@pid)
[114] Fix | Delete
pid = $$
[115] Fix | Delete
unless @pid == pid
[116] Fix | Delete
now = Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond)
[117] Fix | Delete
ary = [now, @pid, pid]
[118] Fix | Delete
OpenSSL::Random.random_add(ary.join("").to_s, 0.0)
[119] Fix | Delete
@pid = pid
[120] Fix | Delete
end
[121] Fix | Delete
return OpenSSL::Random.random_bytes(n)
[122] Fix | Delete
end
[123] Fix | Delete
elsif defined?(AdvApi32)
[124] Fix | Delete
def self.gen_random(n)
[125] Fix | Delete
return AdvApi32.gen_random(n)
[126] Fix | Delete
end
[127] Fix | Delete
[128] Fix | Delete
def self.lastWin32ErrorMessage # :nodoc:
[129] Fix | Delete
# for compatibility
[130] Fix | Delete
return Kernel32.last_error_message
[131] Fix | Delete
end
[132] Fix | Delete
else
[133] Fix | Delete
def self.gen_random(n)
[134] Fix | Delete
flags = File::RDONLY
[135] Fix | Delete
flags |= File::NONBLOCK if defined? File::NONBLOCK
[136] Fix | Delete
flags |= File::NOCTTY if defined? File::NOCTTY
[137] Fix | Delete
begin
[138] Fix | Delete
File.open("/dev/urandom", flags) {|f|
[139] Fix | Delete
unless f.stat.chardev?
[140] Fix | Delete
break
[141] Fix | Delete
end
[142] Fix | Delete
ret = f.read(n)
[143] Fix | Delete
unless ret.length == n
[144] Fix | Delete
raise NotImplementedError, "Unexpected partial read from random device: only #{ret.length} for #{n} bytes"
[145] Fix | Delete
end
[146] Fix | Delete
return ret
[147] Fix | Delete
}
[148] Fix | Delete
rescue Errno::ENOENT
[149] Fix | Delete
end
[150] Fix | Delete
[151] Fix | Delete
raise NotImplementedError, "No random device"
[152] Fix | Delete
end
[153] Fix | Delete
end
[154] Fix | Delete
[155] Fix | Delete
# SecureRandom.hex generates a random hexadecimal string.
[156] Fix | Delete
#
[157] Fix | Delete
# The argument _n_ specifies the length, in bytes, of the random number to be generated.
[158] Fix | Delete
# The length of the resulting hexadecimal string is twice _n_.
[159] Fix | Delete
#
[160] Fix | Delete
# If _n_ is not specified or is nil, 16 is assumed.
[161] Fix | Delete
# It may be larger in future.
[162] Fix | Delete
#
[163] Fix | Delete
# The result may contain 0-9 and a-f.
[164] Fix | Delete
#
[165] Fix | Delete
# p SecureRandom.hex #=> "eb693ec8252cd630102fd0d0fb7c3485"
[166] Fix | Delete
# p SecureRandom.hex #=> "91dc3bfb4de5b11d029d376634589b61"
[167] Fix | Delete
#
[168] Fix | Delete
# If secure random number generator is not available,
[169] Fix | Delete
# NotImplementedError is raised.
[170] Fix | Delete
def self.hex(n=nil)
[171] Fix | Delete
random_bytes(n).unpack("H*")[0]
[172] Fix | Delete
end
[173] Fix | Delete
[174] Fix | Delete
# SecureRandom.base64 generates a random base64 string.
[175] Fix | Delete
#
[176] Fix | Delete
# The argument _n_ specifies the length, in bytes, of the random number
[177] Fix | Delete
# to be generated. The length of the result string is about 4/3 of _n_.
[178] Fix | Delete
#
[179] Fix | Delete
# If _n_ is not specified or is nil, 16 is assumed.
[180] Fix | Delete
# It may be larger in future.
[181] Fix | Delete
#
[182] Fix | Delete
# The result may contain A-Z, a-z, 0-9, "+", "/" and "=".
[183] Fix | Delete
#
[184] Fix | Delete
# p SecureRandom.base64 #=> "/2BuBuLf3+WfSKyQbRcc/A=="
[185] Fix | Delete
# p SecureRandom.base64 #=> "6BbW0pxO0YENxn38HMUbcQ=="
[186] Fix | Delete
#
[187] Fix | Delete
# If secure random number generator is not available,
[188] Fix | Delete
# NotImplementedError is raised.
[189] Fix | Delete
#
[190] Fix | Delete
# See RFC 3548 for the definition of base64.
[191] Fix | Delete
def self.base64(n=nil)
[192] Fix | Delete
[random_bytes(n)].pack("m*").delete("\n")
[193] Fix | Delete
end
[194] Fix | Delete
[195] Fix | Delete
# SecureRandom.urlsafe_base64 generates a random URL-safe base64 string.
[196] Fix | Delete
#
[197] Fix | Delete
# The argument _n_ specifies the length, in bytes, of the random number
[198] Fix | Delete
# to be generated. The length of the result string is about 4/3 of _n_.
[199] Fix | Delete
#
[200] Fix | Delete
# If _n_ is not specified or is nil, 16 is assumed.
[201] Fix | Delete
# It may be larger in future.
[202] Fix | Delete
#
[203] Fix | Delete
# The boolean argument _padding_ specifies the padding.
[204] Fix | Delete
# If it is false or nil, padding is not generated.
[205] Fix | Delete
# Otherwise padding is generated.
[206] Fix | Delete
# By default, padding is not generated because "=" may be used as a URL delimiter.
[207] Fix | Delete
#
[208] Fix | Delete
# The result may contain A-Z, a-z, 0-9, "-" and "_".
[209] Fix | Delete
# "=" is also used if _padding_ is true.
[210] Fix | Delete
#
[211] Fix | Delete
# p SecureRandom.urlsafe_base64 #=> "b4GOKm4pOYU_-BOXcrUGDg"
[212] Fix | Delete
# p SecureRandom.urlsafe_base64 #=> "UZLdOkzop70Ddx-IJR0ABg"
[213] Fix | Delete
#
[214] Fix | Delete
# p SecureRandom.urlsafe_base64(nil, true) #=> "i0XQ-7gglIsHGV2_BNPrdQ=="
[215] Fix | Delete
# p SecureRandom.urlsafe_base64(nil, true) #=> "-M8rLhr7JEpJlqFGUMmOxg=="
[216] Fix | Delete
#
[217] Fix | Delete
# If secure random number generator is not available,
[218] Fix | Delete
# NotImplementedError is raised.
[219] Fix | Delete
#
[220] Fix | Delete
# See RFC 3548 for the definition of URL-safe base64.
[221] Fix | Delete
def self.urlsafe_base64(n=nil, padding=false)
[222] Fix | Delete
s = [random_bytes(n)].pack("m*")
[223] Fix | Delete
s.delete!("\n")
[224] Fix | Delete
s.tr!("+/", "-_")
[225] Fix | Delete
s.delete!("=") unless padding
[226] Fix | Delete
s
[227] Fix | Delete
end
[228] Fix | Delete
[229] Fix | Delete
# SecureRandom.random_number generates a random number.
[230] Fix | Delete
#
[231] Fix | Delete
# If a positive integer is given as _n_,
[232] Fix | Delete
# SecureRandom.random_number returns an integer:
[233] Fix | Delete
# 0 <= SecureRandom.random_number(n) < n.
[234] Fix | Delete
#
[235] Fix | Delete
# p SecureRandom.random_number(100) #=> 15
[236] Fix | Delete
# p SecureRandom.random_number(100) #=> 88
[237] Fix | Delete
#
[238] Fix | Delete
# If 0 is given or an argument is not given,
[239] Fix | Delete
# SecureRandom.random_number returns a float:
[240] Fix | Delete
# 0.0 <= SecureRandom.random_number() < 1.0.
[241] Fix | Delete
#
[242] Fix | Delete
# p SecureRandom.random_number #=> 0.596506046187744
[243] Fix | Delete
# p SecureRandom.random_number #=> 0.350621695741409
[244] Fix | Delete
#
[245] Fix | Delete
def self.random_number(n=0)
[246] Fix | Delete
if 0 < n
[247] Fix | Delete
if defined? OpenSSL::BN
[248] Fix | Delete
OpenSSL::BN.rand_range(n).to_i
[249] Fix | Delete
else
[250] Fix | Delete
hex = n.to_s(16)
[251] Fix | Delete
hex = '0' + hex if (hex.length & 1) == 1
[252] Fix | Delete
bin = [hex].pack("H*")
[253] Fix | Delete
mask = bin[0].ord
[254] Fix | Delete
mask |= mask >> 1
[255] Fix | Delete
mask |= mask >> 2
[256] Fix | Delete
mask |= mask >> 4
[257] Fix | Delete
begin
[258] Fix | Delete
rnd = SecureRandom.random_bytes(bin.length)
[259] Fix | Delete
rnd[0] = (rnd[0].ord & mask).chr
[260] Fix | Delete
end until rnd < bin
[261] Fix | Delete
rnd.unpack("H*")[0].hex
[262] Fix | Delete
end
[263] Fix | Delete
else
[264] Fix | Delete
# assumption: Float::MANT_DIG <= 64
[265] Fix | Delete
if defined? OpenSSL::BN
[266] Fix | Delete
i64 = OpenSSL::BN.rand(64, -1).to_i
[267] Fix | Delete
else
[268] Fix | Delete
i64 = SecureRandom.random_bytes(8).unpack("Q")[0]
[269] Fix | Delete
end
[270] Fix | Delete
Math.ldexp(i64 >> (64-Float::MANT_DIG), -Float::MANT_DIG)
[271] Fix | Delete
end
[272] Fix | Delete
end
[273] Fix | Delete
[274] Fix | Delete
# SecureRandom.uuid generates a v4 random UUID (Universally Unique IDentifier).
[275] Fix | Delete
#
[276] Fix | Delete
# p SecureRandom.uuid #=> "2d931510-d99f-494a-8c67-87feb05e1594"
[277] Fix | Delete
# p SecureRandom.uuid #=> "bad85eb9-0713-4da7-8d36-07a8e4b00eab"
[278] Fix | Delete
# p SecureRandom.uuid #=> "62936e70-1815-439b-bf89-8492855a7e6b"
[279] Fix | Delete
#
[280] Fix | Delete
# The version 4 UUID is purely random (except the version).
[281] Fix | Delete
# It doesn't contain meaningful information such as MAC address, time, etc.
[282] Fix | Delete
#
[283] Fix | Delete
# See RFC 4122 for details of UUID.
[284] Fix | Delete
#
[285] Fix | Delete
def self.uuid
[286] Fix | Delete
ary = self.random_bytes(16).unpack("NnnnnN")
[287] Fix | Delete
ary[2] = (ary[2] & 0x0fff) | 0x4000
[288] Fix | Delete
ary[3] = (ary[3] & 0x3fff) | 0x8000
[289] Fix | Delete
"%08x-%04x-%04x-%04x-%04x%08x" % ary
[290] Fix | Delete
end
[291] Fix | Delete
end
[292] Fix | Delete
[293] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function