|
| 1 | +# = Secure random number generator interface. |
| 2 | +# |
| 3 | +# This library is a interface for secure random number generator which is |
| 4 | +# suitable for HTTP cookies, nonces, etc. |
| 5 | +# |
| 6 | +# It supports following secure random number generators. |
| 7 | +# |
| 8 | +# * openssl |
| 9 | +# * /dev/urandom |
| 10 | +# |
| 11 | +# == Example |
| 12 | +# |
| 13 | +# # random hexadecimal string. |
| 14 | +# p SecureRandom.hex(10) #=> "52750b30ffbc7de3b362" |
| 15 | +# p SecureRandom.hex(10) #=> "92b15d6c8dc4beb5f559" |
| 16 | +# p SecureRandom.hex(11) #=> "6aca1b5c58e4863e6b81b8" |
| 17 | +# p SecureRandom.hex(12) #=> "94b2fff3e7fd9b9c391a2306" |
| 18 | +# p SecureRandom.hex(13) #=> "39b290146bea6ce975c37cfc23" |
| 19 | +# ... |
| 20 | +# |
| 21 | +# # random base64 string. |
| 22 | +# p SecureRandom.base64(10) #=> "EcmTPZwWRAozdA==" |
| 23 | +# p SecureRandom.base64(10) #=> "9b0nsevdwNuM/w==" |
| 24 | +# p SecureRandom.base64(10) #=> "KO1nIU+p9DKxGg==" |
| 25 | +# p SecureRandom.base64(11) #=> "l7XEiFja+8EKEtY=" |
| 26 | +# p SecureRandom.base64(12) #=> "7kJSM/MzBJI+75j8" |
| 27 | +# p SecureRandom.base64(13) #=> "vKLJ0tXBHqQOuIcSIg==" |
| 28 | +# ... |
| 29 | +# |
| 30 | +# # random binary string. |
| 31 | +# p SecureRandom.random_bytes(10) #=> "\016\t{\370g\310pbr\301" |
| 32 | +# p SecureRandom.random_bytes(10) #=> "\323U\030TO\234\357\020\a\337" |
| 33 | +# ... |
| 34 | + |
| 35 | +begin |
| 36 | + require 'openssl' |
| 37 | +rescue LoadError |
| 38 | +end |
| 39 | + |
| 40 | +module SecureRandom |
| 41 | + # SecureRandom.random_bytes generates a random binary string. |
| 42 | + # |
| 43 | + # The argument n specifies the length of the result string. |
| 44 | + # |
| 45 | + # If secure random number generator is not available, |
| 46 | + # NotImplementedError is raised. |
| 47 | + def self.random_bytes(n=nil) |
| 48 | + n ||= 16 |
| 49 | + if defined? OpenSSL::Random |
| 50 | + return OpenSSL::Random.random_bytes(n) |
| 51 | + end |
| 52 | + if !defined?(@has_urandom) || @has_urandom |
| 53 | + @has_urandom = false |
| 54 | + flags = File::RDONLY |
| 55 | + flags |= File::NONBLOCK if defined? File::NONBLOCK |
| 56 | + flags |= File::NOCTTY if defined? File::NOCTTY |
| 57 | + flags |= File::NOFOLLOW if defined? File::NOFOLLOW |
| 58 | + begin |
| 59 | + File.open("/dev/urandom", flags) {|f| |
| 60 | + unless f.stat.chardev? |
| 61 | + raise Errno::ENOENT |
| 62 | + end |
| 63 | + @has_urandom = true |
| 64 | + ret = f.readpartial(n) |
| 65 | + if ret.length != n |
| 66 | + raise NotImplementedError, "Unexpected partial read from random device" |
| 67 | + end |
| 68 | + return ret |
| 69 | + } |
| 70 | + rescue Errno::ENOENT |
| 71 | + raise NotImplementedError, "No random device" |
| 72 | + end |
| 73 | + end |
| 74 | + raise NotImplementedError, "No random device" |
| 75 | + end |
| 76 | + |
| 77 | + # SecureRandom.hex generates a random hex string. |
| 78 | + # |
| 79 | + # The argument n specifies the length of the random length. |
| 80 | + # The length of the result string is twice of n. |
| 81 | + # |
| 82 | + # If secure random number generator is not available, |
| 83 | + # NotImplementedError is raised. |
| 84 | + def self.hex(n=nil) |
| 85 | + random_bytes(n).unpack("H*")[0] |
| 86 | + end |
| 87 | + |
| 88 | + # SecureRandom.base64 generates a random base64 string. |
| 89 | + # |
| 90 | + # The argument n specifies the length of the random length. |
| 91 | + # The length of the result string is about 4/3 of n. |
| 92 | + # |
| 93 | + # If secure random number generator is not available, |
| 94 | + # NotImplementedError is raised. |
| 95 | + def self.base64(n=nil) |
| 96 | + [random_bytes(n)].pack("m*").delete("\n") |
| 97 | + end |
| 98 | + |
| 99 | + # SecureRandom.random_number generates a random number. |
| 100 | + # |
| 101 | + # If an positive integer is given as n, |
| 102 | + # SecureRandom.random_number returns an integer: |
| 103 | + # 0 <= SecureRandom.random_number(n) < n. |
| 104 | + # |
| 105 | + # If 0 is given or an argument is not given, |
| 106 | + # SecureRandom.random_number returns an float: |
| 107 | + # 0.0 <= SecureRandom.random_number() < 1.0. |
| 108 | + def self.random_number(n=0) |
| 109 | + if 0 < n |
| 110 | + hex = n.to_s(16) |
| 111 | + hex = '0' + hex if (hex.length & 1) == 1 |
| 112 | + bin = [hex].pack("H*") |
| 113 | + mask = bin[0].ord |
| 114 | + mask |= mask >> 1 |
| 115 | + mask |= mask >> 2 |
| 116 | + mask |= mask >> 4 |
| 117 | + begin |
| 118 | + rnd = SecureRandom.random_bytes(bin.length) |
| 119 | + rnd[0] = (rnd[0].ord & mask).chr |
| 120 | + end until rnd < bin |
| 121 | + rnd.unpack("H*")[0].hex |
| 122 | + else |
| 123 | + # assumption: Float::MANT_DIG <= 64 |
| 124 | + i64 = SecureRandom.random_bytes(8).unpack("Q")[0] |
| 125 | + Math.ldexp(i64 >> (64-Float::MANT_DIG), -Float::MANT_DIG) |
| 126 | + end |
| 127 | + end |
| 128 | +end |
0 commit comments