Skip to content

Instantly share code, notes, and snippets.

@ijunaid8989
Created May 16, 2016 12:36
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 ijunaid8989/53d6455e2e7bc5fdd0d14551163090f7 to your computer and use it in GitHub Desktop.
Save ijunaid8989/53d6455e2e7bc5fdd0d14551163090f7 to your computer and use it in GitHub Desktop.
defmodule Password do
def generate(mac_address) do
mac_address |> check |> do_generate
end
@regex_for_mac ~r/^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/
defp check(mac) do
if Regex.match?(@regex_for_mac, mac) do
{:ok, mac}
else
{:invalid, "Mac Address is Invalid."}
end
end
defp do_generate({:invalid, message}), do: message
defp do_generate({:ok, mac}), do:
:sha256
|> :crypto.hash(mac)
|> Base.encode16
|> String.slice(0..7)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment