Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a random 16-byte string in the 32-character hexadecimal format

Tags:

erlang

elixir

I am new to Elixir / Erlang. I have been using Ruby for a while and I am wanting to convert this piece of code to Elixir / Erlang.

SecureRandom::random_bytes(16).each_byte.map { |b| sprintf("%02X",b) }.join
like image 297
user15534550 Avatar asked Oct 18 '25 03:10

user15534550


1 Answers

To have the same result, in Elixir you can use the Erlang module :crypto.strong_rand_bytes(16) to generate the random number, and convert it using Base.encode16

Take a look at https://hexdocs.pm/elixir/Base.html to understand better the Base module . Example:

:crypto.strong_rand_bytes(16) |> Base.encode16 # => "4B14868924ACEE98C9C9C404A1F87B08"
like image 95
Vasiliy Ermolovich Avatar answered Oct 21 '25 10:10

Vasiliy Ermolovich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!