CryppoEx v0.1.0 Cryppo View Source

Main public API of Cryppo

Link to this section Summary

Types

Name of an encryption or derivation strategy

Module of an encryption or derivation strategy

Functions

Decrypt encrypted data with an encryption key

List available derivation strategies

Generate an encryption key for an encryption strategy and encrypt data with this encryption key

List available encryption strategies

Generate an encryption key for an encryption strategy

Load various Cryppo data structures from their serialized forms

Serialize various Cryppo data structures as a string

Link to this section Types

Specs

encryption_strategy() :: String.t()

Name of an encryption or derivation strategy

Use Cryppo.encryption_strategies/0 to get a list of encryption strategies. Use Cryppo.derivation_strategies/0 to get a list of derivation strategies.

Link to this type

encryption_strategy_module()

View Source

Specs

encryption_strategy_module() :: atom()

Module of an encryption or derivation strategy

Link to this section Functions

Link to this function

decrypt(encrypted_data, encryption_key_or_raw_key)

View Source

Specs

decrypt(Cryppo.EncryptedData.t(), Cryppo.EncryptionKey.t() | any()) ::
  {:ok, binary()}
  | {:error, :invalid_encryption_key}
  | :decryption_error
  | {:decryption_error, {any(), any()}}
  | {:incompatible_key,
     [submitted_key_strategy: atom(), encryption_strategy: atom()]}

Decrypt encrypted data with an encryption key

Example

iex> {encrypted_data, encryption_key} = Cryppo.encrypt("data to encrypt", "Aes256Gcm")
iex> Cryppo.decrypt(encrypted_data, encryption_key)
{:ok, "data to encrypt"}
Link to this function

decrypt_with_derived_key(encrypted_data_with_derived_key, passphrase)

View Source

Specs

decrypt_with_derived_key(Cryppo.EncryptedDataWithDerivedKey.t(), String.t()) ::
  {:ok, binary(), Cryppo.DerivedKey.t()}
  | :decryption_error
  | {:decryption_error, {any(), any()}}
  | {:incompatible_key,
     [submitted_key_strategy: atom(), encryption_strategy: atom()]}

Decrypt data with a derived key

Example

iex> encrypted = Cryppo.encrypt_with_derived_key("data to encrypt", "Aes256Gcm", "Pbkdf2Hmac", "passphrase")
iex> {:ok, decrypted, _key} = Cryppo.decrypt_with_derived_key(encrypted, "passphrase")
iex> decrypted
"data to encrypt"

Specs

derivation_strategies() :: [encryption_strategy()]

List available derivation strategies

Link to this function

encrypt(data, encryption_strategy)

View Source

Specs

encrypt(binary(), encryption_strategy()) ::
  Cryppo.EncryptedData.t()
  | {:unsupported_encryption_strategy, atom()}
  | :encryption_error

Generate an encryption key for an encryption strategy and encrypt data with this encryption key

Example

iex> {_encrypted_data, _encryption_key} = Cryppo.encrypt("data to encrypt", "Aes256Gcm")
Link to this function

encrypt(data, encryption_strategy, encryption_key_or_raw_key)

View Source

Specs

encrypt(binary(), encryption_strategy(), Cryppo.EncryptionKey.t() | any()) ::
  Cryppo.EncryptedData.t()
  | {:unsupported_encryption_strategy, atom()}
  | {:error, :invalid_encryption_key}
  | :encryption_error
  | {:incompatible_key,
     [submitted_key_strategy: atom(), encryption_strategy: atom()]}

Encrypt data with an encryption key

Example

iex> encryption_key = Cryppo.generate_encryption_key("Aes256Gcm")
iex> _encrypted_data = Cryppo.encrypt("data to encrypt", "Aes256Gcm", encryption_key)

The encryption key must match the encryption strategy:

iex> encryption_key = Cryppo.generate_encryption_key("Aes256Gcm")
iex> Cryppo.encrypt("data to encrypt", "Rsa4096", encryption_key)
{:incompatible_key, [submitted_key_strategy: Cryppo.Aes256gcm, encryption_strategy: Cryppo.Rsa4096]}
Link to this function

encrypt_with_derived_key(data, encryption_strategy, key_derivation_strategy, passphrase)

View Source

Specs

encrypt_with_derived_key(
  binary(),
  encryption_strategy(),
  encryption_strategy(),
  String.t()
) ::
  Cryppo.EncryptedDataWithDerivedKey.t()
  | {:unsupported_encryption_strategy, encryption_strategy()}
  | {:unsupported_key_derivation_strategy, encryption_strategy()}

Encrypt data with a derived key

Example

iex> _encrypted = Cryppo.encrypt_with_derived_key("data to encrypt", "Aes256Gcm", "Pbkdf2Hmac", "passphrase")

Specs

encryption_strategies() :: [encryption_strategy()]

List available encryption strategies

Link to this function

generate_encryption_key(encryption_strategy)

View Source

Specs

generate_encryption_key(encryption_strategy()) ::
  Cryppo.EncryptionKey.t() | {:unsupported_encryption_strategy, binary()}

Generate an encryption key for an encryption strategy

The generated encrypted key is marked as belonging to the encryption strategy.

Example

iex> _encryption_key = Cryppo.generate_encryption_key("Aes256Gcm")

Specs

load(String.t()) ::
  {:ok,
   Cryppo.EncryptedDataWithDerivedKey.t()
   | Cryppo.EncryptedData.t()
   | Cryppo.RsaSignature.t()}
  | {:error, :invalid_yaml, :invalid_bson, :invalid_base64,
     :invalid_derivation_artefacts}
  | {:unsupported_encryption_strategy, binary()}
  | {:unsupported_key_derivation_strategy, binary()}

Load various Cryppo data structures from their serialized forms

3 Cryppo data structures have their own serialization formats:

Cryppo.EncryptedData and Cryppo.EncryptedDataWithDerivedKey have two serialization formats: a legacy format and a more efficient current format. Both formats are loaded by Cryppo.load/1. Cryppo.serialize/2 will serialize structures using the new format.

Examples

iex> s = "Aes256Gcm.WSDb2AmsF7LFOxYb.QUAAAAACYWQABQAAAG5vbmUABWF0ABAAAAAAY9Ck6LzVGiMdiWFK6N5BawVpdgAMAAAAAG_Yxh-I0gGNYoFRigA="
iex> {:ok, %Cryppo.EncryptedData{}} = Cryppo.load(s)

iex> s = "Aes256Gcm.wW4M_sv_kMx14cC6.QUAAAAACYWQABQAAAG5vbmUABWF0ABAAAAAA8Aq84t28sMT9FL8cz-TmMQVpdgAMAAAAANahbwbkfWo18YuCMgA=.Pbkdf2Hmac.SzAAAAAQaQAJUwAABWl2ABQAAAAAuQTqZLVFO49lI6Kx454ffYQ9VV0QbAAgAAAAAA=="
iex> {:ok, %Cryppo.EncryptedDataWithDerivedKey{}} = Cryppo.load(s)

iex> s = "Sign.Rsa4096.V4JbRzpkud-3cHCGqDwGjS3TmRto5Te0iSAtD7oIzsDa83McBDYpU_eeswVZF9AGEvoAEQOCwpqJ_PgbjHKT2nHgLysK-btG6Nxk_K2J7A6Uq15X5QrOgIKTzC00dj1tzAN73u9lsRPKIfwPyp_Mlb6FNs1LoB7OvAusit6QPm8iAwHo4nOWBBUf3hO9b3gsWJ92FxnBsCLYFQj_zv4mnLHj7pDNVtq9Kp4hK6bgcIH4FZtyDKDr6bXEtlCGLDIY10UqNLylkagI36Gyafm-HnD57vRxjgHIGEsd2XcwDJ8PqqrzSYNxl-RyWD3wq0nXE_1rYJ7k1AKLM5G1Hg8B2whqcXpQ52x3zVFCAjlU9GNhT6pdUBxQYw09va7fe2w517PrwwMe90MW87fj3G7dGEKT95cDLTx1d84ybIUFUJOGKY0FF4LL0E3UqWQ92kU4bh-DSTkNmgItX34fiBIOpQDbF238IkRYyFA8LfMPfL-0_dnto9sH0E3Umi41qFvpA2Nq8r57FF4vCOSkXYWVfyitOkY_URqMLxS57azwZRBehJYDtvbqmzaYEDceeLjkxDi--Y10LT4Cz2SGiU--YDJM66PZ3Cp74gvDpsWlohcwYmMib5LrjdtvLOAtOZhoLZyGeeX0lDnwOum7lFRpJd8UIrOlTvpBo48ep2bpmgA=.VmVyaMO8dHVuZyB2ZXJib3Rlbg=="
iex> {:ok, %Cryppo.RsaSignature{}} = Cryppo.load(s)
Link to this function

serialize(s, opts \\ [])

View Source

Specs

Serialize various Cryppo data structures as a string

3 Cryppo data structures have their own serialization formats:

Cryppo.EncryptedData and Cryppo.EncryptedDataWithDerivedKey have two serialization formats: a legacy format and a more efficient current format. Both formats are loaded by Cryppo.load/1. Cryppo.serialize/2 will serialize structures using the new format. In order to serialize a structure using the old format please use Cryppo.serialize(struct, version: :legacy).

Examples

Cryppo.EncryptedData:

iex> {encrypted_data, _key} = Cryppo.encrypt("data to encrypt", "Aes256Gcm")
iex> Cryppo.serialize(encrypted_data)

Cryppo.EncryptedDataWithDerivedKey:

iex> "data to encrypt"
...> |> Cryppo.encrypt_with_derived_key("Aes256Gcm", "Pbkdf2Hmac", "passphrase")
...> |> Cryppo.serialize()

Cryppo.RsaSignature:

iex> private_key = Cryppo.generate_encryption_key("Rsa4096")
iex> "data to encrypt"
...> |> Cryppo.Rsa4096.sign(private_key)
...> |> Cryppo.serialize()