OptimusId v0.1.0 OptimusId.Secret View Source
Holds secret & unique data required for encoding and decoding with the library.
Usage
Start by generating the secret (choose large prime number that fits in 4-byte integer):
secret = OptimusId.Secret.generate(2078493839)
Depending on your application and approach towards security you may choose one of following approaches towards storing the newly generated secret:
use the tuple representation directly (e.g. if the secret will be hardcoded into source code or stored in database with support for Erlang terms like ETS)
store string representation yielded by
OptimusId.Secret.to_string/1and convert it back to tuple representation (e.g. if using env vars or arbitrary secret vault)
For improved security you may choose to use multiple secrets (e.g. one per API resource).
Link to this section Summary
Functions
Generates new random secret based on 32-bit prime number.
Converts secret tuple to string representation (useful for storage in env var or vault).
Link to this section Types
t()
View Source
t() :: {t_prime(), t_inverse_prime(), t_xor_modifier()}
t() :: {t_prime(), t_inverse_prime(), t_xor_modifier()}
t_inverse_prime()
View Source
t_inverse_prime() :: OptimusId.integer_32bit()
t_inverse_prime() :: OptimusId.integer_32bit()
t_prime()
View Source
t_prime() :: OptimusId.integer_32bit()
t_prime() :: OptimusId.integer_32bit()
t_xor_modifier()
View Source
t_xor_modifier() :: OptimusId.integer_32bit()
t_xor_modifier() :: OptimusId.integer_32bit()
Link to this section Functions
generate(prime)
View Source
generate(OptimusId.integer_32bit()) :: t()
generate(OptimusId.integer_32bit()) :: t()
Generates new random secret based on 32-bit prime number.
Note that generated secret will be different even for the same input prime number.
to_string(tuple) View Source
Converts secret tuple to string representation (useful for storage in env var or vault).
Note that OptimusId doesn't use the string version for performance reasons and since libraries are
not available in config/*.exs you'll have to convert it yourself e.g. with following code in
config/releases.exs:
secret_string = System.fetch_env!("MY_SECRET")
secret_tuple =
secret_string
|> String.split("-")
|> Enum.map(&String.to_integer/1)
|> List.to_tuple
config :my_app, :my_secret, secret_tuple