Available

amalgame-crypto

Hashing, HMAC, AES-GCM, JWS signatures, passwords, TOTP.

amc package add crypto GitHub ↗ ← Back to the ecosystem

Overview

SHA-256, HMAC and SHA-1 are written in the package. AES-GCM, signatures, scrypt and ECDH go through OpenSSL 3's libcrypto: you need its headers to install the package (libssl-dev on Debian and Ubuntu). Bytes are a List<int>.

Features

  • ✓Sha256 (FIPS 180-4) and Hmac.Sha256 (RFC 2104), as bytes or hex
  • ✓Aead: AES-256-GCM authenticated encryption; SealAes256GcmAuto draws a fresh nonce on each call
  • ✓JwsKey: ES256 and RS256 signatures (JWS, RFC 7515), PEM and JWK keys, WebAuthn public keys (COSE)
  • ✓Password.Hash / Verify: scrypt with a random salt, constant-time comparison
  • ✓Totp: one-time codes (RFC 6238), compatible with Google Authenticator
  • ✓Ecdh P-256 and AES-128-GCM: the building blocks of Web Push encryption (RFC 8291)
  • ✓Bytes: text, hex and base64url conversions, constant-time comparison

Limits

  • –OpenSSL 3 is required, even for SHA-256 alone: the whole package links to libcrypto.
  • –AES-128-GCM: encryption only, no decryption.
  • –No HKDF: build it with Hmac.Sha256.
  • –Base32: decoding only (for TOTP secrets).
  • –No Argon2id or Ed25519.

Example

After amc package add crypto, put this code in main.am and run amc run main.am.

import Amalgame.Collections
import Amalgame.Crypto

public class Program {
    public static void Main() {
        // SHA-256 and HMAC-SHA-256, as hex
        Console.WriteLine(Sha256.OfString("hello"))
        // 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
        Console.WriteLine(Hmac.Sha256OfStrings("secret-key", "payload"))

        // Password: scrypt with a random salt, constant-time check
        let stored: string = Password.Hash("correct horse")
        if (Password.Verify("correct horse", stored)) {
            Console.WriteLine("password ok")
        }

        // ES256 signature (JWS): sign, then verify
        let key: JwsKey = JwsKey.GenerateEs256()
        let msg: List<int> = Bytes.FromString("payload")
        let sig: List<int> = key.Sign(msg)
        if (key.Verify(msg, sig)) {
            Console.WriteLine("signature ok: " + Bytes.ToBase64Url(sig))
        }
    }
}

At a glance

Related packages