From 65ecb9f01876bb1a7c2530c0df888f45f5a11cbb Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Sat, 11 Feb 2017 18:34:25 -0600 Subject: [PATCH] Add Compute md5 Digest Of A String as an Elixir til --- README.md | 3 ++- elixir/compute-md5-digest-of-a-string.md | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 elixir/compute-md5-digest-of-a-string.md diff --git a/README.md b/README.md index 5af3ca2..7e4794f 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really warrant a full blog post. These are mostly things I learn by pairing with smart people at [Hashrocket](http://hashrocket.com/). -_501 TILs and counting..._ +_502 TILs and counting..._ --- @@ -87,6 +87,7 @@ _501 TILs and counting..._ - [Assert An Exception Is Raised](elixir/assert-an-exception-is-raised.md) - [Binary Representation Of A String](elixir/binary-representation-of-a-string.md) - [Check For A Substring Match](elixir/check-for-a-substring-match.md) +- [Compute md5 Digest Of A String](elixir/compute-md5-digest-of-a-string.md) - [Counting Records With Ecto](elixir/counting-records-with-ecto.md) - [Create A Date With The Date Sigil](elixir/create-a-date-with-the-date-sigil.md) - [Creating Indexes With Ecto](elixir/creating-indexes-with-ecto.md) diff --git a/elixir/compute-md5-digest-of-a-string.md b/elixir/compute-md5-digest-of-a-string.md new file mode 100644 index 0000000..f6e7638 --- /dev/null +++ b/elixir/compute-md5-digest-of-a-string.md @@ -0,0 +1,20 @@ +# Compute md5 Digest Of A String + +To compute the md5 digest of a string, we can use Erlang's top-level `md5` +function. + +```elixir +> :erlang.md5("#myelixirstatus") +<<145, 148, 139, 99, 194, 176, 105, 18, 242, 246, 37, 69, 142, 69, 226, 199>> +``` + +This, however, gives us the result in the raw binary representation. We +would like it in a base 16 encoding, as md5 digests tend to be. + +We can wrap (or pipe) this with `Base.encode16` to get the result we are +looking for. + +```elixir +> Base.encode16(:erlang.md5("#myelixirstatus"), case: :lower) +"91948b63c2b06912f2f625458e45e2c7" +```