Skip to content

Wrapper for the Subtle Crypto Web API

Notifications You must be signed in to change notification settings

mint-lang/mint-crypto

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Crypto

A comprehensive cryptographic library for Mint with support for encryption, signing, hashing, and key derivation.

Features

  • Random Generation: Cryptographically secure bytes, UUIDs, integers
  • Key Generation: AES, ECDSA, ECDH, RSA with multiple curves
  • Key Derivation: PBKDF2, HKDF, ECDH key agreement
  • Encryption: AES-GCM, AES-CBC, AES-CTR, RSA-OAEP
  • Hashing: SHA-1, SHA-256, SHA-384, SHA-512, HMAC
  • Encoding: Hex, Base64, Base64URL
  • Signing: ECDSA, RSA-PSS, RSASSA

Basic Usage

// Encrypt a string with a password
case await Crypto.encrypt("Hello World", "password") {
  Ok(encrypted) => dbg "Encrypted successfully"
  Err(error) => dbg "Encryption failed"
}
// Generate a key and encrypt data
case await Crypto.generateKey {
  Err => dbg "Error"

  Ok(key) =>
    case await Crypto.encryptWithKey("data", key) {
      Ok(encrypted) => dbg "Encrypted with key"
      Err => dbg "Error"
    }
}

Working with Bytes

This library uses Array(Number) to represent binary data, where each number is a byte (0-255).

The Crypto.Encoding Module

The Crypto.Encoding module provides functions to convert between bytes and various formats:

  • Base64 encoding: toBase64, fromBase64, toBase64Url, fromBase64Url
  • String conversion: stringToBytes, bytesToString
  • Hex encoding: toHex, fromHex

Dual API Pattern

Most cryptographic functions come in two variants:

  1. Byte functions - Work directly with Array(Number)
  2. String functions - Accept strings and return strings
// Using bytes directly
case await Crypto.Hash.digest([72, 101, 108, 108, 111], Crypto.HashAlgorithm.SHA256) {
  Ok(hashBytes) => dbg Crypto.Encoding.toHex(hashBytes)
  Err(error) => dbg "Error"
}

// Using string variant
case await Crypto.Hash.digestStringHex("Hello", Crypto.HashAlgorithm.SHA256) {
  Ok(hex) => dbg hex  // Same result as above
  Err(error) => dbg "Error"
}

Converting Between Formats

// String to bytes
let bytes = Crypto.Encoding.stringToBytes("Hello")
// [72, 101, 108, 108, 111]

// Bytes to hex
let hex = Crypto.Encoding.toHex(bytes)
// "48656c6c6f"

// Hex back to bytes
case Crypto.Encoding.fromHex("48656c6c6f") {
  Ok(decoded) => dbg decoded  // [72, 101, 108, 108, 111]
  Err(error) => dbg "Invalid hex"
}

// Bytes to base64
let base64 = Crypto.Encoding.toBase64(bytes)
// "SGVsbG8="

Testing

Run all tests:

mint test

No External Dependencies

All cryptography uses the Web Crypto API built into modern browsers.

About

Wrapper for the Subtle Crypto Web API

Resources

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published

Languages