Skip to content

Instantly share code, notes, and snippets.

@ijunaid8989
Last active May 4, 2016 08:12
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/73461ac8d2f8ed3ff53e07a431893c9a to your computer and use it in GitHub Desktop.
Save ijunaid8989/73461ac8d2f8ed3ff53e07a431893c9a to your computer and use it in GitHub Desktop.
defmodule Password do
@regex_for_mac ~r/^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/
def generate(mac_address) do
case check_mac(mac_address) do
{:invalid, message} ->
message
{:ok} ->
password =
Base.encode16(:crypto.hash(:sha256, mac_address))
|> String.slice(0..7)
end
end
defp check_mac(mac) do
cond do
!Regex.match?(@regex_for_mac, mac) ->
{:invalid, "Mac Address is Invalid."}
true ->
{:ok}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment