oxdeca blog

Create secure random secrets

RNG = '/dev/random'

def create_secret( length=32 )

  secret = []

  for i in 1..length do

    byte = File.read( RNG, 1 )

    while( byte.ord < 33 || byte.ord > 126 )

      byte = File.read( RNG, 1 )

    end

    secret << byte

  end

  secret.join

end