Created
May 16, 2016 12:36
-
-
Save ijunaid8989/53d6455e2e7bc5fdd0d14551163090f7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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