defmodule Pbkdf2.Base64 do @moduledoc """ Module that provides base64 encoding for pbkdf2. Most developers will not need to use this module directly. Pbkdf2 uses an adapted base64 alphabet (using `.` instead of `+` and with no padding). """ import Bitwise b64_alphabet = Enum.with_index('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./') for {encoding, value} <- b64_alphabet do defp unquote(:enc64)(unquote(value)), do: unquote(encoding) defp unquote(:dec64)(unquote(encoding)), do: unquote(value) end @doc """ Encode using the adapted Pbkdf2 alphabet. ## Examples iex> Pbkdf2.Base64.encode("spamandeggs") "c3BhbWFuZGVnZ3M" """ def encode(<<>>), do: <<>> def encode(data) do split = 3 * div(byte_size(data), 3) <> = data main = for <>, into: <<>>, do: <> case rest do <> -> <> <> -> <> <<>> -> main end end @doc """ Decode using the adapted Pbkdf2 alphabet. ## Examples iex> Pbkdf2.Base64.decode("c3BhbWFuZGVnZ3M") "spamandeggs" """ def decode(<<>>), do: <<>> def decode(data) do split = 4 * div(byte_size(data), 4) <> = data main = for <>, into: <<>>, do: <> case rest do <> -> <> <> -> <> <<>> -> main end end end