Rails - ActiveSupport :: SecureRandom.hex - Retrieving SET Character Count

I want to create a secure random 45-digit key. I tried:

ActiveSupport::SecureRandom.hex(45)

But it returns more than 45 characters, it is in the range of 60+. Any suggestions? How can you create a securerandom for a given number of characters?

thank

+3
source share
1 answer

From the documentation:

The argument n indicates the length of a random length. The result string is two times longer than n.

So, if you want 45, you can set it to 23, which will return 46 characters and then take the first 45. Of course, the first five characters of the long string will work too.

ActiveSupport::SecureRandom.hex(23)[0...45]
+4
source

Source: https://habr.com/ru/post/1789307/


All Articles