Skip to content

Commit 65ecb9f

Browse files
committed
Add Compute md5 Digest Of A String as an Elixir til
1 parent 17db6bc commit 65ecb9f

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really
77
warrant a full blog post. These are mostly things I learn by pairing with
88
smart people at [Hashrocket](http://hashrocket.com/).
99

10-
_501 TILs and counting..._
10+
_502 TILs and counting..._
1111

1212
---
1313

@@ -87,6 +87,7 @@ _501 TILs and counting..._
8787
- [Assert An Exception Is Raised](elixir/assert-an-exception-is-raised.md)
8888
- [Binary Representation Of A String](elixir/binary-representation-of-a-string.md)
8989
- [Check For A Substring Match](elixir/check-for-a-substring-match.md)
90+
- [Compute md5 Digest Of A String](elixir/compute-md5-digest-of-a-string.md)
9091
- [Counting Records With Ecto](elixir/counting-records-with-ecto.md)
9192
- [Create A Date With The Date Sigil](elixir/create-a-date-with-the-date-sigil.md)
9293
- [Creating Indexes With Ecto](elixir/creating-indexes-with-ecto.md)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Compute md5 Digest Of A String
2+
3+
To compute the md5 digest of a string, we can use Erlang's top-level `md5`
4+
function.
5+
6+
```elixir
7+
> :erlang.md5("#myelixirstatus")
8+
<<145, 148, 139, 99, 194, 176, 105, 18, 242, 246, 37, 69, 142, 69, 226, 199>>
9+
```
10+
11+
This, however, gives us the result in the raw binary representation. We
12+
would like it in a base 16 encoding, as md5 digests tend to be.
13+
14+
We can wrap (or pipe) this with `Base.encode16` to get the result we are
15+
looking for.
16+
17+
```elixir
18+
> Base.encode16(:erlang.md5("#myelixirstatus"), case: :lower)
19+
"91948b63c2b06912f2f625458e45e2c7"
20+
```

0 commit comments

Comments
 (0)