forked from seven1m/30-days-of-elixir
-
Notifications
You must be signed in to change notification settings - Fork 1
/
23-digest.exs
35 lines (30 loc) · 992 Bytes
/
23-digest.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Just playing with some digest stuff.
#
# To run:
#
# $ erlc support/sha1.erl && mv sha1.beam support/
# $ elixir 23-digest.exs
#
# Observations:
#
# * Erlang/Elixir don't have a sha1 method built-in???
# * Erlang/Elixir don't have a built-in binary-to-hex conversion???
# * Loading Erlang code was a bit weird. I should have figured out
# how to compile erl files within the code (not the terminal)
#
# Dude, where's my expansive standard library?!
:code.load_abs('support/sha1')
ExUnit.start
defmodule MiscTest do
use ExUnit.Case
test "sha1" do
# Erlang doesn't have sha1 built-in, so had to import code from
# our friend Mr. Nicolas Favre-Felix
assert :sha1.hexstring('foo') == '0BEEC7B5EA3F0FDBC95D0DD47F3C5BC275DA8A33'
end
test "md5" do
# Erlang has crypto built-in for md5, but no way natively to convert to hex
# so again Mr. Felix to the rescue!
assert :crypto.hash(:md5, 'foo') |> :sha1.bin2hex == 'ACBD18DB4CC2F85CEDEF654FCCC4A4D8'
end
end