Method: SecureRandom.hex

Defined in:
lib/securerandom.rb

.hex(n = nil) ⇒ Object

SecureRandom.hex generates a random hexadecimal string.

The argument n specifies the length, in bytes, of the random number to be generated. The length of the resulting hexadecimal string is twice n.

If n is not specified or is nil, 16 is assumed. It may be larger in future.

The result may contain 0-9 and a-f.

p SecureRandom.hex #=> "eb693ec8252cd630102fd0d0fb7c3485"
p SecureRandom.hex #=> "91dc3bfb4de5b11d029d376634589b61"

If secure random number generator is not available, NotImplementedError is raised.



134
135
136
# File 'lib/securerandom.rb', line 134

def self.hex(n=nil)
  random_bytes(n).unpack("H*")[0]
end