Youtube-style IDs in Elixir

identifiers
Tags: #<Tag:0x00007f8ea61bf800>

#1

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 @idiot 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"

#2

:+1:

You can use padding: false instead probably.

:crypto.strong_rand_bytes(8)
|> Base.url_encode64(padding: false)

#3

Ah, good catch :slight_smile: