Packages

Typed distributed messaging for Gleam on the BEAM.

Retired package: Deprecated - The project needs to be redesigned around a much smaller and clearer core.

Current section

Files

Jump to
distribute src distribute@crypto@adapter.erl
Raw

src/distribute@crypto@adapter.erl

-module(distribute@crypto@adapter).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/distribute/crypto/adapter.gleam").
-export([default_options/1, development_options/1]).
-export_type([crypto_adapter/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" Crypto Adapter contract and utilities.\n"
"\n"
" This module defines the behaviour contract that all crypto providers\n"
" must implement, along with helper functions for creating default options.\n"
"\n"
" ## Adapter Contract\n"
"\n"
" A crypto provider handles:\n"
"\n"
" - Lifecycle management (init/shutdown)\n"
" - Key exchange handshake (start/continue)\n"
" - Secure context management\n"
" - Encryption and decryption\n"
" - Key rotation (rekeying)\n"
" - Health monitoring\n"
"\n"
" ## Implementing a Provider\n"
"\n"
" To implement a custom provider, create a module that provides a function\n"
" returning a `CryptoAdapter` record with all required functions:\n"
"\n"
" ```gleam\n"
" pub fn new() -> CryptoAdapter {\n"
" CryptoAdapter(\n"
" init: my_init,\n"
" shutdown: my_shutdown,\n"
" handshake_start: my_handshake_start,\n"
" handshake_continue: my_handshake_continue,\n"
" secure_context: my_secure_context,\n"
" encrypt: my_encrypt,\n"
" decrypt: my_decrypt,\n"
" rekey: my_rekey,\n"
" health: my_health,\n"
" metrics: my_metrics,\n"
" )\n"
" }\n"
" ```\n"
"\n"
" See `distribute/crypto/noop_adapter` for a reference implementation.\n"
).
-type crypto_adapter() :: {crypto_adapter,
fun((distribute@crypto@types:provider_options()) -> {ok,
distribute@crypto@types:provider_handle()} |
{error, distribute@crypto@types:crypto_error()}),
fun((distribute@crypto@types:provider_handle()) -> {ok, nil} |
{error, distribute@crypto@types:crypto_error()}),
fun((distribute@crypto@types:provider_handle(), binary(), binary(), gleam@option:option(distribute@crypto@types:handshake_message())) -> {ok,
distribute@crypto@types:handshake_result()} |
{error, distribute@crypto@types:crypto_error()}),
fun((distribute@crypto@types:provider_handle(), distribute@crypto@types:handshake_state(), distribute@crypto@types:handshake_message()) -> {ok,
distribute@crypto@types:handshake_result()} |
{error, distribute@crypto@types:crypto_error()}),
fun((distribute@crypto@types:provider_handle(), binary()) -> gleam@option:option(distribute@crypto@types:secure_context())),
fun((distribute@crypto@types:provider_handle(), distribute@crypto@types:secure_context(), bitstring()) -> {ok,
bitstring()} |
{error, distribute@crypto@types:crypto_error()}),
fun((distribute@crypto@types:provider_handle(), distribute@crypto@types:secure_context(), bitstring()) -> {ok,
bitstring()} |
{error, distribute@crypto@types:crypto_error()}),
fun((distribute@crypto@types:provider_handle(), binary()) -> {ok, nil} |
{error, distribute@crypto@types:crypto_error()}),
fun((distribute@crypto@types:provider_handle()) -> distribute@crypto@types:health_status()),
fun((distribute@crypto@types:provider_handle()) -> distribute@crypto@types:crypto_metrics())}.
-file("src/distribute/crypto/adapter.gleam", 103).
?DOC(
" Create default provider options.\n"
"\n"
" Provides sensible defaults:\n"
" - is_development: False\n"
" - key_rotation_interval_ms: 0 (no auto-rotation)\n"
" - handshake_timeout_ms: 30000 (30 seconds)\n"
).
-spec default_options(binary()) -> distribute@crypto@types:provider_options().
default_options(Name) ->
{provider_options, Name, false, 0, 30000, maps:new()}.
-file("src/distribute/crypto/adapter.gleam", 117).
?DOC(
" Create development provider options.\n"
"\n"
" **Warning:** These options are for development only and should\n"
" never be used in production. The provider will be marked as insecure.\n"
).
-spec development_options(binary()) -> distribute@crypto@types:provider_options().
development_options(Name) ->
{provider_options, Name, true, 0, 5000, maps:new()}.