Current section

Files

Jump to
ex_kits lib crypto common.ex
Raw

lib/crypto/common.ex

defmodule ExKits.Crypto.Common do
@moduledoc false
@doc """
md5
"""
@spec md5(String.t()) :: binary()
def md5(plaintext), do: :crypto.hash(:md5, plaintext)
@doc """
sha1
"""
@spec sha(String.t()) :: binary()
def sha(plaintext), do: :crypto.hash(:sha, plaintext)
@doc """
sha256
"""
@spec sha256(String.t()) :: binary()
def sha256(plaintext), do: :crypto.hash(:sha256, plaintext)
@doc """
generate random string
## Example
iex(17)> Common.Crypto.random_string 16
"2jqDlUxDuOt-qyyZ"
"""
@spec random_string(integer()) :: String.t()
def random_string(length) do
length
|> :crypto.strong_rand_bytes()
|> Base.url_encode64()
|> binary_part(0, length)
end
end