Just a bit of fun, today I found out that Youtube IDs are just url-safe base-64 truncated to 11 characters. And it turns out that Elixir ships with just the right in-built functions to make that work:
:crypto.strong_rand_bytes(8)
|> Base.url_encode64()
|> String.slice(0, 11)
# => "NxdgQMuCZ5Q"
EDIT:
Or, as @idi527 pointed out below, if you pass padding: false
to the url_encode64()
call, you don’t need the String.slice()
:
:crypto.strong_rand_bytes(8)
|> Base.url_encode64(padding: false)
# => "NxdgQMuCZ5Q"