tom_crypto library

Cryptographic utilities for secure authentication and data protection.

This library provides a complete set of cryptographic primitives:

  • JWT Tokens: Token-based authentication with HMAC/RSA signing
  • Password Hashing: Secure password storage with Argon2
  • RSA Encryption: Asymmetric encryption with OAEP padding
  • RSA Key Management: Key generation, PEM parsing/encoding

Quick Start

import 'package:tom_crypto/tom_crypto.dart';

// Hash a password
final (hash, spec) = TomPasswordHasher.hashPassword('userPassword123');

// Create a JWT token
final token = TomServerJwtToken(
  {'userId': '123', 'role': 'admin'},
  encryptedData: {'permissions': ['read', 'write', 'delete']},
  expiresIn: Duration(hours: 24),
);

// Generate RSA keys
final secureRandom = RsaKeyHelper.getSecureRandom();
final keyPair = await RsaKeyHelper.computeRSAKeyPair(secureRandom);

Classes

Audience
Audience claim. Can contains one or more audience entry, used like a list
JWT
JWTAlgorithm
JWTKey
RsaKeyHelper
Helper class for RSA key generation, parsing, and encoding.
RSAPrivateKey
Private keys in RSA
RSAPublicKey
Public keys in RSA
SecretKey
For HMAC algorithms
TomClientJwtToken
Client-side JWT token parser and validator.
TomJwtConfiguration
Configuration for JWT token signing and encryption.
TomPasswordHasher
Secure password hashing and verification using Argon2.
TomProtectedValue
A secret in memory: reveal gives the plaintext, digest identifies it.
TomSecureBytes
Draws cryptographically secure random bytes.
TomServerJwtToken
Server-side JWT token generator.

Properties

tomRsaHashIdentifier ↔ String
OID for SHA-256 hash algorithm used in RSA signatures.
getter/setter pair

Functions

getRsaKeyPair(SecureRandom secureRandom) → AsymmetricKeyPair<PublicKey, PrivateKey>
Generates an RSA key pair synchronously.
rsaDecrypt(RSAPrivateKey myPrivate, Uint8List cipherText) → Uint8List
Decrypts RSA-encrypted data using OAEP padding.
rsaEncrypt(RSAPublicKey myPublic, Uint8List dataToEncrypt) → Uint8List
Encrypts data using RSA with OAEP padding.
rsaSign(RSAPrivateKey privateKey, Uint8List dataToSign) → Uint8List
Creates an RSA digital signature using SHA-256.
rsaVerify(RSAPublicKey publicKey, Uint8List signedData, Uint8List signature) → bool
Verifies an RSA digital signature.

Exceptions / Errors

TomJwtTokenException
Exception thrown when JWT token operations fail.