Skip to main content

This is a new website theme. Help me improve it and give your feedback (opens in a new tab).

Functional Cryptography

Published:

Tags:

Cryptography Programming Elixir
This blog post is more than two years old. It is preserved here in the hope that it is useful to someone, but please be aware that links may be broken and that opinions expressed here may not reflect my current views. If this is a technical article, it may no longer reflect current best practice.

I’m pretty good at buying books and not finding the time to read them. I did manage to finish The Manga Guide to Cryptography and have now started on Introducing Elixir. In order to make sure I’m understanding these topics, I’ve decided to implement the algorithms I’ve read about in the former, using the programming language I’m reading about in the latter.

I was pleased to find that installing Elixir on Debian is really easy:

$ sudo apt install elixir

I’ve not got that far into the Elixir book, but I do already have an implementation of a Caeser shift. I have no idea if this is idiomatic or not, but it works:

defmodule Caesar do
  def shift(message, key \\ 3) do
    shift(to_charlist(message), "", key)
  end

  defp shift([head | tail], interim, key) do
    cp = head + key
    interim = interim <> <<cp>>
    shift(tail, interim, key)
  end

  defp shift([], interim, _key) do
    interim
  end
end

When I run it in iex(1) I get:

iex(57)> Caesar.shift("hello")
"khoor"

I’ve not yet dealt with things like running out of the end of the alphabet yet (z + 3 = ?) which I guess I will do with guards.

It’s a little frustrating that things are not working the way I expect them to, and I’m very concious that there’s a whole load of classes of problem that I would have no idea how to tackle yet. I feel like I’m learning an entirely new thing compared to learning Python after Java where much of the syntax is similar and you have the same paradigms. I think this is a good thing.