1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* lib/secrand.rb: rename SecRand() to SecRand.random_number.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12497 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2007-06-08 21:40:05 +00:00
parent 3a868301ea
commit 7d061438f1
2 changed files with 34 additions and 27 deletions

View file

@ -1,3 +1,7 @@
2007-06-08 Tanaka Akira <akr@fsij.org>
* lib/secrand.rb: rename SecRand() to SecRand.random_number.
Fri Jun 8 16:34:20 2007 Tanaka Akira <akr@fsij.org> Fri Jun 8 16:34:20 2007 Tanaka Akira <akr@fsij.org>
* ext/zlib/zlib.c (gzfile_s_open): use FilePathValue to support * ext/zlib/zlib.c (gzfile_s_open): use FilePathValue to support

View file

@ -1,7 +1,7 @@
# = Secure random number generator interface. # = Secure random number generator interface.
# #
# This library is a interface for secure random number generator which is # This library is a interface for secure random number generator which is
# suitable for HTTP cookies, etc. # suitable for HTTP cookies, nonces, etc.
# #
# It supports following secure random number generators. # It supports following secure random number generators.
# #
@ -16,6 +16,7 @@
# p SecRand.hex(11) #=> "6aca1b5c58e4863e6b81b8" # p SecRand.hex(11) #=> "6aca1b5c58e4863e6b81b8"
# p SecRand.hex(12) #=> "94b2fff3e7fd9b9c391a2306" # p SecRand.hex(12) #=> "94b2fff3e7fd9b9c391a2306"
# p SecRand.hex(13) #=> "39b290146bea6ce975c37cfc23" # p SecRand.hex(13) #=> "39b290146bea6ce975c37cfc23"
# ...
# #
# # random base64 string. # # random base64 string.
# p SecRand.base64(10) #=> "EcmTPZwWRAozdA==" # p SecRand.base64(10) #=> "EcmTPZwWRAozdA=="
@ -94,32 +95,34 @@ module SecRand
def self.base64(n=nil) def self.base64(n=nil)
[random_bytes(n)].pack("m*").delete("\n") [random_bytes(n)].pack("m*").delete("\n")
end end
end
# SecRand() generates a random number. # SecRand.random_number generates a random number.
# #
# If an positive integer is given as n, # If an positive integer is given as n,
# SecRand() returns an integer: 0 <= SecRand(n) < n. # SecRand.random_number returns an integer:
# # 0 <= SecRand.random_number(n) < n.
# If 0 is given or an argument is not given, #
# SecRand() returns an float: 0.0 <= SecRand() < 1.0. # If 0 is given or an argument is not given,
def SecRand(n=0) # SecRand.random_number returns an float:
if 0 < n # 0.0 <= SecRand.random_number() < 1.0.
hex = n.to_s(16) def self.random_number(n=0)
hex = '0' + hex if (hex.length & 1) == 1 if 0 < n
bin = [hex].pack("H*") hex = n.to_s(16)
mask = bin[0].ord hex = '0' + hex if (hex.length & 1) == 1
mask |= mask >> 1 bin = [hex].pack("H*")
mask |= mask >> 2 mask = bin[0].ord
mask |= mask >> 4 mask |= mask >> 1
begin mask |= mask >> 2
rnd = SecRand.random_bytes(bin.length) mask |= mask >> 4
rnd[0] = (rnd[0].ord & mask).chr begin
end until rnd < bin rnd = SecRand.random_bytes(bin.length)
rnd.unpack("H*")[0].hex rnd[0] = (rnd[0].ord & mask).chr
else end until rnd < bin
# assumption: Float::MANT_DIG <= 64 rnd.unpack("H*")[0].hex
i64 = SecRand.random_bytes(8).unpack("Q")[0] else
Math.ldexp(i64 >> (64-Float::MANT_DIG), -Float::MANT_DIG) # assumption: Float::MANT_DIG <= 64
i64 = SecRand.random_bytes(8).unpack("Q")[0]
Math.ldexp(i64 >> (64-Float::MANT_DIG), -Float::MANT_DIG)
end
end end
end end