defmodule ZBase32 do @moduledoc ~S""" Z-Base-32: human-oriented base-32 encoding http://philzimmermann.com/docs/human-oriented-base-32-encoding.txt """ use Bitwise def encode(<<>>), do: <<>> def encode(data) when is_binary(data) do split = 5 * div(byte_size(data), 5) <> = data main = for <>, into: <<>>, do: <> case rest do <> -> <> <> -> <> <> -> <> <> -> <> <<>> -> main end end def decode(<<>>), do: <<>> def decode(string) when is_binary(string) do split = byte_size(string) - rem(byte_size(string), 8) <> = string main = for <>, into: <<>>, do: <> case rest do <> -> <> <> -> <> <> -> <> <> -> <> <> -> <> <<>> -> main end end alphabet = Enum.with_index 'ybndrfg8ejkmcpqxot1uwisza345h769' for {encoding, value} <- alphabet do defp enc(unquote(value)), do: unquote(encoding) defp dec(unquote(encoding)), do: unquote(value) end defp dec(c) do raise ArgumentError, "non-alphabet digit found: #{<>}" end end