Sha256: 07f6cf891a19211bc4b8f973b577937548960968431287bc8ffe1d743caf39ab

Contents?: true

Size: 842 Bytes

Versions: 86

Compression:

Stored size: 842 Bytes

Contents

defmodule Atbash do
  @key Enum.zip(?a..?z, ?z..?a) |> Enum.into(%{})

  @doc """
  Encode a given plaintext to the corresponding ciphertext

  ## Examples

  iex> Atbash.encode("completely insecure")
  "xlnko vgvob rmhvx fiv"
  """
  @spec encode(String.t) :: String.t
  def encode(plaintext) do
    plaintext |> normalize |> transpose |> chunk |> Enum.join(" ")
  end

  @spec decode(String.t) :: String.t
  def decode(cipher) do
    cipher |> normalize |> transpose
  end

  defp normalize(input) do
    Regex.replace(~r{\W}, String.downcase(input), "")
  end

  defp transpose(text) do
    text
    |> String.to_char_list
    |> Enum.map(&convert/1)
    |> List.to_string
  end

  defp convert(character) do
    Map.get(@key, character, character)
  end

  defp chunk(input) do
    Regex.scan(~r(.{1,5}), input) |> List.flatten
  end
end

Version data entries

86 entries across 86 versions & 1 rubygems

Version Path
trackler-2.2.1.47 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.2.1.46 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.2.1.45 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.2.1.44 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.2.1.43 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.2.1.42 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.2.1.41 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.2.1.40 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.2.1.39 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.2.1.38 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.2.1.37 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.2.1.36 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.2.1.35 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.2.1.34 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.2.1.33 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.2.1.32 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.2.1.31 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.2.1.30 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.2.1.29 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.2.1.28 tracks/elixir/exercises/atbash-cipher/example.exs