Skip to content

Instantly share code, notes, and snippets.

@KristerV
Created December 22, 2016 20:13
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 KristerV/39797c6938c5f01560845869c8822cfa to your computer and use it in GitHub Desktop.
Save KristerV/39797c6938c5f01560845869c8822cfa to your computer and use it in GitHub Desktop.
defmodule Arvuti do
def hashit(str, ind, result) do
hash = :crypto.hash(:md5 , str <> to_string(ind)) |> Base.encode16()
{pos, _} = :string.to_integer(to_char_list(String.slice(hash,5..5)))
letter = String.slice(hash, 6..6)
cond do
isdone(result) ->
result
String.slice(hash, 0..4) == "00000" && 0 <= pos && pos < 8 && String.slice(result, pos..pos) == "_" ->
result = String.slice(result, 0..pos-1) <> letter <> String.slice(result, pos+1..10)
IO.puts result
hashit(str, ind+1, result)
true ->
hashit(str, ind+1, result)
end
end
def isdone(code) do
String.slice(code, 0..0) != "_" && isdone(String.slice(code, 1..10))
end
end
# IO.puts Arvuti.hashit("abc", 3231928, "________")
IO.puts Arvuti.hashit("wtnhxymk", 0, "________")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment