Skip to content

Conversation

@jteich
Copy link

@jteich jteich commented Sep 22, 2022

No description provided.

# Generate a random key
def random_key(length=256)
_random_seed.unpack('H*')[0][0..((length/8)-1)]
_random_seed.unpack('H*')[0][0..((length/4)-1)]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The key as generated by _random_seed is a hex string. In a hex string, each character encodes 4 bits.

else
chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
(1..size).collect{|a| chars[rand(chars.size)] }.join
(1..size).collect{ rand(256).chr }.join
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using a modulo based upon a-z,A-Z,0-9 which comprises 62 characters, this effectively yields log2(62) (about 5.95) bits per character of randomness. rand(256) will yield a full 8 bits per character, inline with what OpenSSL::Random.random_bytes would return.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant