Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ gleam add casper@1

```gleam
import casper
import gleam/crypto

pub fn main() -> Nil {
// Generate a 32 byte encryption key
let key = crypto.strong_random_bytes(32)
let key = casper.generate_key()

// This will fail if the key isn't the right size, i.e. 32 bytes
let assert Ok(encrypted) = casper.encrypt(<<"casper">>, key)
Expand Down
5 changes: 5 additions & 0 deletions src/casper.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
//// libraries. The "with" variants allow you to add additional authenticated
//// data AAD which is required for encryption and decryption.

/// Generate 32 random bytes for use in the ChaCha20-Poly1305 cipher.
@external(erlang, "casper_ffi", "generate_key")
@external(javascript, "./casper_ffi.mjs", "generate_key")
pub fn generate_key() -> BitArray

@external(erlang, "casper_ffi", "encrypt_internal")
@external(javascript, "./casper_ffi.mjs", "encrypt_internal")
fn encrypt_internal(input: BitArray, key: BitArray) -> BitArray
Expand Down
4 changes: 4 additions & 0 deletions src/casper_ffi.erl
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
-module(casper_ffi).

-export([
generate_key/0,
encrypt_internal/2,
decrypt_internal/2,
encrypt_with_internal/3,
decrypt_with_internal/3
]).

generate_key() ->
crypto:strong_rand_bytes(32).

encrypt_internal(Value, Key) ->
Iv = crypto:strong_rand_bytes(12),
{Encrypted, Tag} = crypto:crypto_one_time_aead(
Expand Down
6 changes: 5 additions & 1 deletion src/casper_ffi.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import * as crypto from "node:crypto";
function strongRandomBytes(n) {
return crypto.randomBytes(n);
}


export function generate_key() {
return new BitArray(crypto.randomBytes(32));
}

export function encrypt_internal(input, key) {
const iv = strongRandomBytes(12);
const cipher = crypto.createCipheriv('chacha20-poly1305', key.rawBuffer, iv);
Expand Down
6 changes: 3 additions & 3 deletions test/casper_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn main() -> Nil {

pub fn basic_roundtrip_no_associated_data_test() {
let input = <<"casper test">>
let key = crypto.strong_random_bytes(32)
let key = casper.generate_key()

let encrypted =
casper.encrypt(input, key)
Expand All @@ -26,7 +26,7 @@ pub fn basic_roundtrip_no_associated_data_test() {
pub fn basic_roundtrip_with_associated_data_test() {
let input = <<"casper test">>
let associated = <<"casper associated">>
let key = crypto.strong_random_bytes(32)
let key = casper.generate_key()

let encrypted =
casper.encrypt_with(input, associated, key)
Expand All @@ -43,7 +43,7 @@ pub fn basic_roundtrip_with_associated_data_test() {
pub fn fails_when_decrypt_associated_data_differs_test() {
let input = <<"casper test">>
let associated = <<"casper associated">>
let key = crypto.strong_random_bytes(32)
let key = casper.generate_key()

let encrypted =
casper.encrypt_with(input, associated, key)
Expand Down