-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from Glutexo/hash
✨ Hash the filename using MD5
- Loading branch information
Showing
4 changed files
with
102 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
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 |
This file contains 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
This file contains 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
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 |
This file contains 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