Skip to content

Commit

Permalink
Merge pull request #50 from Glutexo/hash
Browse files Browse the repository at this point in the history
✨ Hash the filename using MD5
  • Loading branch information
Glutexo authored Mar 26, 2022
2 parents 048e7f5 + 723f731 commit f2e5457
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 6 deletions.
18 changes: 18 additions & 0 deletions lib/hash.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule Hash do
def md5(data, fmt) do
hash(:md5, data)
|> format(fmt)
end

def format(data, :bin) do
data
end

def format(data, :hex) do
Base.encode16(data, case: :lower)
end

def hash(func, data) do
:crypto.hash(func, data)
end
end
2 changes: 1 addition & 1 deletion lib/onigumo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ defmodule Onigumo do
end

def create_file_name(url) do
Base.url_encode64(url, padding: false)
Hash.md5(url, :hex)
end
end
81 changes: 81 additions & 0 deletions test/hash_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
defmodule HashTest do
use ExUnit.Case

@known_md5s [
{
"",
"d41d8cd98f00b204e9800998ecf8427e",
<<212, 29, 140, 217, 143, 0, 178, 4, 233, 128, 9, 152, 236, 248, 66, 126>>
},
{
"onigumo",
"3d8425b6ea2efe0fa78075492c719ffe",
<<61, 132, 37, 182, 234, 46, 254, 15, 167, 128, 117, 73, 44, 113, 159, 254>>
},
{
"https://www.example.com/",
"dcbfe5ad9e8af3495ca4582e364c1bce",
<<220, 191, 229, 173, 158, 138, 243, 73, 92, 164, 88, 46, 54, 76, 27, 206>>
}
]

@binary_hash <<61, 132, 37, 182, 234, 46, 254, 15, 167, 128, 117, 73, 44, 113, 159, 254>>
@formatted_hashes [
{
:bin,
<<61, 132, 37, 182, 234, 46, 254, 15, 167, 128, 117, 73, 44, 113, 159, 254>>
},
{
:hex,
"3d8425b6ea2efe0fa78075492c719ffe"
}
]

@known_hash_data "onigumo"
@known_hashes [
{
:md5,
<<61, 132, 37, 182, 234, 46, 254, 15, 167, 128, 117, 73, 44, 113, 159, 254>>
},
{
:sha256,
<<233, 125, 4, 183, 163, 127, 108, 247, 107, 107, 129, 176, 45, 233, 210, 255, 218, 34, 202,
51, 112, 158, 160, 220, 15, 109, 229, 143, 188, 196, 45, 128>>
},
{
:sha512,
<<215, 171, 58, 63, 123, 94, 7, 206, 21, 30, 63, 150, 208, 35, 179, 69, 235, 190, 128, 183,
0, 89, 237, 183, 155, 8, 190, 178, 233, 240, 157, 95, 187, 200, 110, 163, 116, 55, 57, 63,
73, 16, 192, 76, 15, 236, 126, 106, 117, 209, 199, 43, 231, 192, 105, 122, 247, 100, 47,
100, 178, 231, 31, 217>>
}
]

test("hash MD5 known value in hexadecimal") do
for {data, hash_hex, _} <- @known_md5s do
hash = Hash.md5(data, :hex)
assert(hash == hash_hex)
end
end

test("hash MD5 known value in binary") do
for {data, _, hash_bin} <- @known_md5s do
hash = Hash.md5(data, :bin)
assert(hash == hash_bin)
end
end

test("format a binary hash") do
for {format, hash} <- @formatted_hashes do
formatted = Hash.format(@binary_hash, format)
assert(formatted == hash)
end
end

test("hash a known value") do
for {func, known_hash} <- @known_hashes do
computed_hash = Hash.hash(func, @known_hash_data)
assert(computed_hash == known_hash)
end
end
end
7 changes: 2 additions & 5 deletions test/onigumo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,8 @@ defmodule OnigumoTest do
input_url = "https://onigumo.local/hello.html"
created_file_name = Onigumo.create_file_name(input_url)

expected_file_name = Base.url_encode64(input_url, padding: false)
expected_file_name = Hash.md5(input_url, :hex)
assert(created_file_name == expected_file_name)

unexpected_file_name = Base.url_encode64(input_url, padding: true)
assert(created_file_name != unexpected_file_name)
end

defp prepare_response(url) do
Expand All @@ -118,7 +115,7 @@ defmodule OnigumoTest do
end

defp assert_downloaded(url, tmp_dir) do
file_name = Base.url_encode64(url, padding: false)
file_name = Onigumo.create_file_name(url)
output_path = Path.join(tmp_dir, file_name)
read_output = File.read!(output_path)
expected_output = body(url)
Expand Down

0 comments on commit f2e5457

Please sign in to comment.