Pegasus

Package Version Hex Docs

Wide logo

A modern cryptography library for Gleam, built with safety and ease of use in mind.

gleam add pegasus_crypto

Features

Pegasus is backed by the Orion Rust cryptography library (via NIFs), providing high-performance cryptographic primitives.

Examples

Hashing

import pegasus/hash
import gleam/bit_array

pub fn main() {
  let data = bit_array.from_string("Hello, world!")
  
  // Create a secure hash
  let assert Ok(digest) = hash.digest(data)
  
  // Use digest...
}

Authentication

import pegasus/authentication
import gleam/bit_array

pub fn main() {
  let data = bit_array.from_string("Message to authenticate")
  
  // Create a key from raw bytes
  let key_data = bit_array.from_string("AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH")
  let key = authentication.key_from_bitarray(key_data)
  
  // Create authentication tag
  let assert Ok(tag) = authentication.authenticate(data, key)
  
  // Verify the authentication tag
  let assert Ok(_) = authentication.verify(data, key, tag)
}

Encryption

import pegasus/xchacha20
import gleam/bit_array

pub fn main() {
  let plaintext = bit_array.from_string("Secret message")
  
  // Generate key and nonce
  let assert Ok(key) = xchacha20.generate_key()
  let assert Ok(nonce) = xchacha20.generate_nonce()
  
  // Encrypt data
  let assert Ok(ciphertext) = xchacha20.encrypt(key, nonce, plaintext)
  
  // Decrypt data
  let assert Ok(decrypted) = xchacha20.decrypt(key, nonce, ciphertext)
}

Safety

Pegasus inherits strong security properties from Orion:

Additionally, Pegasus emphasizes safety through:

License

Pegasus is MIT licensed. The underlying cryptographic library, Orion, is also MIT licensed.

Roadmap

Pegasus aims to support all cryptographic primitives available in the Orion library:

Development

gleam run   # Run the project
gleam test  # Run the tests
cd native && cargo build --release  # Build Rust components
cp native/target/release/libpegasus_native.so priv/  # Copy NIF to priv directory for Erlang

Further documentation can be found at https://hexdocs.pm/pegasus.

Search Document