Skip to content

Commit 11d54ae

Browse files
committed
Add crypto module docs, simple_hash, joken create_signer, generate_and_sign, , verify_and_validate
1 parent f422074 commit 11d54ae

File tree

4 files changed

+73
-5
lines changed

4 files changed

+73
-5
lines changed

lib/helper/crypto.ex

+57-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
defmodule MishkaDeveloperTools.Helper.Crypto do
22
@moduledoc """
3+
In reality, this module serves as a support for other libraries in addition
4+
to Erlang's built-in functions for encryption, hashing, and other topics that are associated
5+
with the language.
36
7+
It should be brought to your attention that certain functions necessitate the addition of their
8+
dependencies to the primary project. Consequently, prior to making use of these functionalities,
9+
establish the appropriate dependence within the project in accordance with your requirements.
410
"""
11+
alias Postgrex.Extensions.JSON
512
@type based32_url :: <<_::64, _::_*8>>
613

7-
@simple_hash_algs %{
14+
@hash_algs %{
815
"RS256" => %{"type" => :asymmetric, "hash_algorithm" => :sha256, "binary_size" => 16},
916
"RS384" => %{"type" => :asymmetric, "hash_algorithm" => :sha384, "binary_size" => 24},
1017
"RS512" => %{"type" => :asymmetric, "hash_algorithm" => :sha512, "binary_size" => 32},
@@ -13,6 +20,8 @@ defmodule MishkaDeveloperTools.Helper.Crypto do
1320
"HS512" => %{"type" => :symmetric, "hash_algorithm" => :sha512, "binary_size" => 32}
1421
}
1522

23+
@hash_algs_keys Map.keys(@hash_algs)
24+
1625
@doc """
1726
Generate a binary composed of random bytes.
1827
@@ -407,9 +416,52 @@ defmodule MishkaDeveloperTools.Helper.Crypto do
407416
408417
- https://github.com/malach-it/boruta_auth/blob/master/lib/boruta/oauth/schemas/client.ex#L173
409418
"""
410-
def simple_hash(text, alg, truncated \\ nil) do
411-
:crypto.hash(@simple_hash_algs[alg]["hash_algorithm"], text)
412-
|> binary_part(0, truncated || @simple_hash_algs[alg]["binary_size"])
413-
|> Base.url_encode64(padding: false)
419+
def simple_hash(text, alg, truncated \\ nil) when alg in @hash_algs_keys do
420+
hashed =
421+
:crypto.hash(@hash_algs[alg]["hash_algorithm"], text)
422+
|> binary_part(0, truncated || @hash_algs[alg]["binary_size"])
423+
424+
{Base.url_encode64(hashed, padding: false), hashed}
425+
end
426+
427+
def simple_hash(rand_size \\ 32) do
428+
token = :crypto.strong_rand_bytes(rand_size)
429+
hashed = :crypto.hash(:sha256, token)
430+
431+
{Base.url_encode64(token, padding: false), hashed}
432+
end
433+
434+
if Code.ensure_loaded?(JSON) and Code.ensure_loaded?(Joken) do
435+
defmodule Token do
436+
use Joken.Config
437+
end
438+
439+
def create_signer(alg, key) when alg in @hash_algs_keys do
440+
Joken.Signer.create(alg, key)
441+
end
442+
443+
def generate_and_sign(extra_claims, signer \\ nil) do
444+
if !is_nil(signer),
445+
do: Token.generate_and_sign(extra_claims, signer),
446+
else: Token.generate_and_sign(extra_claims)
447+
end
448+
449+
def generate_and_sign!(extra_claims, signer \\ nil) do
450+
if !is_nil(signer),
451+
do: Token.generate_and_sign!(extra_claims, signer),
452+
else: Token.generate_and_sign!(extra_claims)
453+
end
454+
455+
def verify_and_validate(token, signer \\ nil) do
456+
if !is_nil(signer),
457+
do: Token.verify_and_validate(token, signer),
458+
else: Token.verify_and_validate(token)
459+
end
460+
461+
def verify_and_validate!(token, signer \\ nil) do
462+
if !is_nil(signer),
463+
do: Token.verify_and_validate!(token, signer),
464+
else: Token.verify_and_validate!(token)
465+
end
414466
end
415467
end

lib/helper/extra.ex

+11
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,15 @@ defmodule MishkaDeveloperTools.Helper.Extra do
6666
|> String.to_charlist()
6767
|> Enum.all?(&(&1 in allowed_chars))
6868
end
69+
70+
def get_unix_time() do
71+
DateTime.utc_now() |> DateTime.to_unix()
72+
end
73+
74+
def get_unix_time_with_shift(number, type \\ :second)
75+
when type in [:day, :hour, :minute, :second] do
76+
DateTime.utc_now()
77+
|> DateTime.add(number, type)
78+
|> DateTime.to_unix()
79+
end
6980
end

mix.exs

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ defmodule MishkaDeveloperTools.MixProject do
4848
{:ex_url, "~> 2.0", optional: true},
4949
{:ex_phone_number, "~> 0.4.3", optional: true},
5050
{:nimble_totp, "~> 1.0", optional: true},
51+
{:joken, "~> 2.6", optional: true},
52+
{:jason, "~> 1.4", optional: true},
5153
# Make sure you have a C compiler installed. See the Comeonin wiki for details.
5254
# Wiki link: https://github.com/riverrun/comeonin/wiki/Requirements
5355
{:bcrypt_elixir, "~> 3.1", optional: true},

mix.lock

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
"ex_phone_number": {:hex, :ex_phone_number, "0.4.4", "8e994abe583496a3308cf56af013dca9b47a0424b0a9940af41cb0d66b848dd3", [:mix], [{:sweet_xml, "~> 0.7", [hex: :sweet_xml, repo: "hexpm", optional: false]}], "hexpm", "a59875692ec57b3392959a7740f3e9a5cb08da88bcaee4efd480c770f5bb0f2c"},
1717
"ex_url": {:hex, :ex_url, "2.0.0", "c940f034895103fc493948ef0036c9473c396988dad3835531fb0c1476ba63c4", [:mix], [{:ex_cldr, "~> 2.18", [hex: :ex_cldr, repo: "hexpm", optional: true]}, {:ex_phone_number, "~> 0.1", [hex: :ex_phone_number, repo: "hexpm", optional: true]}, {:gettext, "~> 0.13", [hex: :gettext, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:nimble_parsec, "~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "5bba73e551462f891a3cc47d7c1a42acff8dc0cb4dce02f03c937947db4f0d31"},
1818
"html_sanitize_ex": {:hex, :html_sanitize_ex, "1.4.3", "67b3d9fa8691b727317e0cc96b9b3093be00ee45419ffb221cdeee88e75d1360", [:mix], [{:mochiweb, "~> 2.15 or ~> 3.1", [hex: :mochiweb, repo: "hexpm", optional: false]}], "hexpm", "87748d3c4afe949c7c6eb7150c958c2bcba43fc5b2a02686af30e636b74bccb7"},
19+
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
20+
"joken": {:hex, :joken, "2.6.1", "2ca3d8d7f83bf7196296a3d9b2ecda421a404634bfc618159981a960020480a1", [:mix], [{:jose, "~> 1.11.9", [hex: :jose, repo: "hexpm", optional: false]}], "hexpm", "ab26122c400b3d254ce7d86ed066d6afad27e70416df947cdcb01e13a7382e68"},
21+
"jose": {:hex, :jose, "1.11.9", "c861eb99d9e9f62acd071dc5a49ffbeab9014e44490cd85ea3e49e3d36184777", [:mix, :rebar3], [], "hexpm", "b5ccc3749d2e1638c26bed806259df5bc9e438797fe60dc71e9fa0716133899b"},
1922
"makeup": {:hex, :makeup, "1.1.1", "fa0bc768698053b2b3869fa8a62616501ff9d11a562f3ce39580d60860c3a55e", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "5dc62fbdd0de44de194898b6710692490be74baa02d9d108bc29f007783b0b48"},
2023
"makeup_elixir": {:hex, :makeup_elixir, "0.16.2", "627e84b8e8bf22e60a2579dad15067c755531fea049ae26ef1020cad58fe9578", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "41193978704763f6bbe6cc2758b84909e62984c7752b3784bd3c218bb341706b"},
2124
"makeup_erlang": {:hex, :makeup_erlang, "0.1.5", "e0ff5a7c708dda34311f7522a8758e23bfcd7d8d8068dc312b5eb41c6fd76eba", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "94d2e986428585a21516d7d7149781480013c56e30c6a233534bedf38867a59a"},

0 commit comments

Comments
 (0)