Project

General

Profile

Feature #10849 ยป securerandom.patch

View differences:

lib/securerandom.rb
297 297
    ary[3] = (ary[3] & 0x3fff) | 0x8000
298 298
    "%08x-%04x-%04x-%04x-%04x%08x" % ary
299 299
  end
300

  
301
  # SecureRandom.alphanumeric generates a random alphanumeric string.
302
  #
303
  # The argument _n_ specifies the length, in bytes, of the random number
304
  # to be generated. The length of the result string is about 4/3 of _n_.
305
  #
306
  # If _n_ is not specified or is nil, 16 is assumed.
307
  # It may be larger in the future.
308
  #
309
  # The result may contain A-Z, a-z and 0-9.
310
  #
311
  #   p SecureRandom.alphanumeric #=> "2BuBuLf3WfSKyQbRccA"
312
  #   p SecureRandom.alphanumeric #=> "6BbW0pxO0YENxn38HMUbcQ"
313
  #
314
  # If a secure random number generator is not available,
315
  # +NotImplementedError+ is raised.
316
  def self.alphanumeric(n=nil)
317
    [random_bytes(n)].pack("m*").delete("\n+/=")
318
  end
300 319
end
test/test_securerandom.rb
164 164
    end
165 165
  end
166 166

  
167
  def test_alphanumeric
168
    65.times do |idx|
169
      an = @it.alphanumeric
170
      assert_match(/^[0-9a-zA-Z]+$/, an)
171
    end
172
  end
173

  
167 174
  def protect
168 175
    begin
169 176
      yield