mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
1.8 compatibility for random_number method on SecureRandom.
1.9 has its own version.
This commit is contained in:
parent
923eb9569c
commit
1dfebd4f0d
2 changed files with 6 additions and 2 deletions
|
@ -164,13 +164,13 @@ module ActiveSupport
|
||||||
hex = n.to_s(16)
|
hex = n.to_s(16)
|
||||||
hex = '0' + hex if (hex.length & 1) == 1
|
hex = '0' + hex if (hex.length & 1) == 1
|
||||||
bin = [hex].pack("H*")
|
bin = [hex].pack("H*")
|
||||||
mask = bin[0].ord
|
mask = bin[0]
|
||||||
mask |= mask >> 1
|
mask |= mask >> 1
|
||||||
mask |= mask >> 2
|
mask |= mask >> 2
|
||||||
mask |= mask >> 4
|
mask |= mask >> 4
|
||||||
begin
|
begin
|
||||||
rnd = SecureRandom.random_bytes(bin.length)
|
rnd = SecureRandom.random_bytes(bin.length)
|
||||||
rnd[0] = (rnd[0].ord & mask).chr
|
rnd[0] = rnd[0] & mask
|
||||||
end until rnd < bin
|
end until rnd < bin
|
||||||
rnd.unpack("H*")[0].hex
|
rnd.unpack("H*")[0].hex
|
||||||
else
|
else
|
||||||
|
|
|
@ -12,4 +12,8 @@ class SecureRandomTest < Test::Unit::TestCase
|
||||||
b2 = ActiveSupport::SecureRandom.hex(64)
|
b2 = ActiveSupport::SecureRandom.hex(64)
|
||||||
assert_not_equal b1, b2
|
assert_not_equal b1, b2
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_random_number
|
||||||
|
assert ActiveSupport::SecureRandom.random_number(5000) < 5000
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue