Skip to content

Instantly share code, notes, and snippets.

@azer
Created October 26, 2022 04:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azer/cb1b25a8ca61fedce0806c1425da4c20 to your computer and use it in GitHub Desktop.
Save azer/cb1b25a8ca61fedce0806c1425da4c20 to your computer and use it in GitHub Desktop.
defmodule HashID do
@coder Hashids.new(
salt: "HmPbtapoe1FGfTFbEEeZcWKuakIQp3L0",
min_en: 6
)
def encode(id, type) do
type =
type
|> to_string
|> :erlang.binary_to_list()
|> Kernel.++([id])
|> Enum.reverse()
Hashids.encode(@coder, type)
end
def decode(data) do
Hashids.decode(@coder, data)
|> case do
{:ok, [head | tail]} ->
type =
tail
|> Enum.reverse()
|> :erlang.list_to_binary()
|> String.to_atom()
{head, type}
_ ->
:error
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment