adamu
Secure Random Numbers in Elixir (and Erlang)
I’ve been working with a problem involving cryptography recently, which requires cryptographically secure random numbers. This post is my exploration into how to generate secure random numbers with Elixir.
Most Liked
adamu
Playing with the concept, we could make a super simple wrapper library that provides sensible defaults.
defmodule Crypto do
def random_bytes(n) do
:crypto.strong_rand_bytes(n)
end
def random_integer(n) do
{int, _unused_seed} = :rand.uniform_s(n, :crypto.rand_seed_s())
int
end
def random_float() do
{float, _unused_seed} = :rand.uniform_real_s(:crypto.rand_seed_s())
float
end
end
iex(1)> Crypto.random_bytes(10)
<<231, 232, 192, 181, 115, 153, 116, 24, 141, 158>>
iex(2)> Crypto.random_integer(10)
6
iex(3)> Crypto.random_float()
0.21388785742247599
2
walkr
Nice writeup. Thanks for sharing.
1
Popular in Blog Posts
What I genuinely value at my workplace is that I can easily explore new languages through internal mobility. Throughout my career within ...
New
Posted via Devtalk (see this thread for details).
New
This post asks if we can remove Alpine from the PETAL stack. Can we do everything we need with just LiveView? Also, let’s explore an area...
New
Whatever your Nerves project does, there’s a good chance that it can be enhanced by securely connecting to a Phoenix Server in the cloud;...
New
Elixir has a built-in Zip library that comes with OTP. This post explores how to use the zip module and asks the important question: “Is ...
New
Dialyzer is a tool that you’ve probably heard about in the Elixir community. You may have even used it. However, adding Dialyzer to an ex...
New
I think <span class="hashtag-icon-placeholder"></span>liveview is going to be a big topic in the foreseeable future, so wondering whether...
New
I ran into an interesting problem recently where simple concurrency on the BEAM via Task.async made my application a lot slower and a lot...
New
Does the world need another How to create a blog article?
Maybe not.
But then again, creating something out of nothing is what we love....
New
This post is a guide on how Norba and I are building LiveMatch, a real-time app for soccer to follow multiple games in one place.
New
Other popular topics
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
I will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> someth...
New
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New







