Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions src/casper.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn decrypt_with_internal(
key: BitArray,
) -> Result(BitArray, Nil)

/// Given a `BitArray` and a 32 byte encryption key, generated via
/// Given a `BitArray` message and a 32 byte encryption key, generated via
/// `gleam/crypto` `strong_random_bytes` method, encrypt the input via
/// ChaCha20-Poly1305. The output `BitArray` is encoded especially for this
/// cipher.
Expand All @@ -40,7 +40,10 @@ fn decrypt_with_internal(
///
/// The javascript target uses Node's crypto library and will not work on the
/// web.
pub fn encrypt(input: BitArray, key: BitArray) -> Result(BitArray, Nil) {
pub fn encrypt(
message input: BitArray,
key key: BitArray,
) -> Result(BitArray, Nil) {
case key {
<<encryption_key:bytes-size(32)>> -> {
Ok(encrypt_internal(input, encryption_key))
Expand All @@ -49,12 +52,12 @@ pub fn encrypt(input: BitArray, key: BitArray) -> Result(BitArray, Nil) {
}
}

/// See `encrypt`. This method additionally accepts additional authenticated
/// data.
/// See `encrypt`. This method adds additional authenticated data to encrypt
/// the message.
pub fn encrypt_with(
input: BitArray,
associated_data: BitArray,
key: BitArray,
message input: BitArray,
associated_data associated_data: BitArray,
key key: BitArray,
) -> Result(BitArray, Nil) {
case key {
<<encryption_key:bytes-size(32)>> -> {
Expand All @@ -64,24 +67,27 @@ pub fn encrypt_with(
}
}

/// Given a `BitArray` (the output from `encrypt`) and a 32 byte encryption key
/// attempt to decrypt the input.
/// Given a `BitArray` message (the output from `encrypt`) and a 32 byte
/// encryption key attempt to decrypt the input.
///
/// This method will return `Error(Nil)` if it is unable to decrypt the input.
/// This will happen if you don't call `encrypt` first.
///
/// The javascript target uses Node's crypto library and will not work on the
/// web.
pub fn decrypt(input: BitArray, key: BitArray) -> Result(BitArray, Nil) {
pub fn decrypt(
message input: BitArray,
key key: BitArray,
) -> Result(BitArray, Nil) {
decrypt_internal(input, key)
}

/// See `decrypt`. This method additionally accepts additional authenticated
/// data.
/// See `decrypt`. This method adds additional authenticated data to decrypt
/// the message.
pub fn decrypt_with(
input: BitArray,
associated_data: BitArray,
key: BitArray,
message input: BitArray,
associated_data associated_data: BitArray,
key key: BitArray,
) -> Result(BitArray, Nil) {
decrypt_with_internal(input, associated_data, key)
}