Last active
May 4, 2016 08:12
-
-
Save ijunaid8989/73461ac8d2f8ed3ff53e07a431893c9a 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 | |
@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