Current section
Files
Jump to
Current section
Files
lib/ex_ring_ring/infrastructure/hash_algorithms.ex
defmodule ExRingRing.Infrastructure.HashAlgorithms do
@moduledoc """
Hash algorithms registry and factory.
"""
@algorithms %{
md5: ExRingRing.Infrastructure.HashAlgorithms.MD5,
sha: ExRingRing.Infrastructure.HashAlgorithms.SHA,
sha256: ExRingRing.Infrastructure.HashAlgorithms.SHA256,
crc32: ExRingRing.Infrastructure.HashAlgorithms.CRC32,
phash2: ExRingRing.Infrastructure.HashAlgorithms.PHash2
}
@doc """
Gets the hash algorithm module.
"""
@spec get_algorithm(atom()) :: module()
def get_algorithm(algo) do
Map.fetch!(@algorithms, algo)
end
@doc """
Checks if algorithm is supported.
"""
@spec supported?(atom()) :: boolean()
def supported?(algo), do: Map.has_key?(@algorithms, algo)
@doc """
Gets list of supported algorithms.
"""
@spec supported_algorithms() :: [atom()]
def supported_algorithms, do: Map.keys(@algorithms)
@doc """
Calculates hash using the specified algorithm.
"""
@spec hash(atom(), term()) :: non_neg_integer()
def hash(algo, data) do
get_algorithm(algo).hash(data)
end
@doc """
Gets hash byte size for algorithm.
"""
@spec hash_byte_size(atom()) :: pos_integer()
def hash_byte_size(algo) do
get_algorithm(algo).hash_byte_size()
end
end