cryptdart 0.1.3 copy "cryptdart: ^0.1.3" to clipboard
cryptdart: ^0.1.3 copied to clipboard

CryptDart is a unified cryptography library for Dart, providing easy-to-use interfaces for symmetric and asymmetric encryption, digital signatures (including ECDSA), and key management. Built on Point [...]

CryptDart #

pub package license Dart

CryptDart is a comprehensive, unified cryptography library for Dart that provides easy-to-use interfaces for symmetric/asymmetric encryption, digital signatures, key exchange protocols, and secure session management. Built on PointyCastle and BasicUtils with a focus on developer experience and security best practices.

๐Ÿš€ Key Features #

๐Ÿ” Complete Cryptographic Suite #

  • Symmetric Encryption: AES, ChaCha20, DES with multiple modes
  • Asymmetric Encryption: RSA with configurable key sizes
  • Digital Signatures: HMAC, RSA signatures, ECDSA (EdDSA support planned)
  • Key Exchange: ECDH with multiple curves (secp256r1, secp384r1, secp521r1)

๐Ÿ—๏ธ Clean Architecture #

  • Three-layer design: Interfaces โ†’ Partial Implementations โ†’ Concrete Classes
  • Unified interfaces: Consistent API across all algorithms
  • Expiration management: Built-in key and cipher expiration handling
  • Centralized utilities: No code duplication, easy maintenance

๐Ÿ”„ Secure Session Management #

  • Bidirectional ECDH sessions: Automatic key exchange and algorithm negotiation
  • Forward secrecy: Ephemeral keys for each session
  • Algorithm agility: Dynamic selection of best available algorithms
  • Easy integration: High-level factory methods for common use cases

๐Ÿงช Production Ready #

  • Comprehensive tests: 100+ test cases covering all scenarios
  • Strong typing: Full Dart 3.0+ null safety and type safety
  • Error handling: Robust error management and validation
  • Documentation: Extensive examples and API documentation

๐Ÿ“ฆ Installation #

Add to your pubspec.yaml:

dependencies:
  cryptdart: ^0.1.2

Run:

dart pub get

๐ŸŽฏ Quick Start #

Basic Symmetric Encryption #

import 'package:cryptdart/cryptdart.dart';

void main() async {
  // Generate a secure AES key
  final aesKey = AESCipher.generateKey();
  print('Generated AES key: ${aesKey.substring(0, 16)}...');

  // Create cipher with expiration
  final cipher = AESCipher((
    parent: (
      key: aesKey,
      parent: (
        parent: (
          algorithm: CryptoAlgorithm.aes,
          expirationDate: DateTime.now().add(Duration(hours: 24)),
          expirationTimes: null,
        ),
      ),
    ),
  ));

  // Encrypt data
  final data = 'Hello, secure world! ๐Ÿ”';
  final encrypted = cipher.encrypt(data.codeUnits);
  final decrypted = cipher.decrypt(encrypted);
  
  print('Original: $data');
  print('Decrypted: ${String.fromCharCodes(decrypted)}');
  print('Cipher expires: ${cipher.expirationDate}');
}

Asymmetric Encryption & Digital Signatures #

import 'package:cryptdart/cryptdart.dart';

void main() async {
  // Generate RSA key pair
  final keyPair = await RSACipher.generateKeyPair(bitLength: 2048);
  print('Generated RSA ${keyPair['publicKey']!.contains('BEGIN PUBLIC KEY') ? 'โœ“' : 'โœ—'}');

  // RSA Encryption
  final rsaCipher = RSACipher((
    parent: (
      publicKey: keyPair['publicKey']!,
      privateKey: keyPair['privateKey']!,
      parent: (
        parent: (
          algorithm: CryptoAlgorithm.rsa,
          expirationDate: DateTime.now().add(Duration(days: 30)),
          expirationTimes: null,
        ),
      ),
    ),
  ));

  final message = 'Secret message ๐Ÿคซ';
  final encrypted = await rsaCipher.encrypt(message.codeUnits);
  final decrypted = await rsaCipher.decrypt(encrypted);
  print('RSA decrypted: ${String.fromCharCodes(decrypted)}');

  // RSA Digital Signature
  final signature = RSASignatureCipher((
    parent: (
      publicKey: keyPair['publicKey']!,
      privateKey: keyPair['privateKey']!,
      parent: (
        parent: (
          algorithm: CryptoAlgorithm.rsaSignature,
          expirationDate: DateTime.now().add(Duration(days: 30)),
          expirationTimes: null,
        ),
      ),
    ),
  ));

  final signData = 'Document to sign';
  final sig = await signature.sign(signData.codeUnits);
  final verified = await signature.verifySignature(signData.codeUnits, sig);
  print('Signature verified: $verified โœ“');
}

ECDH Key Exchange & Secure Sessions #

import 'package:cryptdart/cryptdart.dart';
import 'dart:convert';

void main() async {
  print('๐Ÿ”„ Setting up secure ECDH communication...\n');

  // High-level secure session establishment
  final aliceSession = await SecureCommunicationFactory.initiateSecureSession(
    localPeerId: 'alice@example.com',
    supportedAsymmetric: [CryptoAlgorithm.rsa],
    supportedSymmetric: [CryptoAlgorithm.chacha20, CryptoAlgorithm.aes],
    sendToRemote: (initMessage) async {
      print('๐Ÿ“ค Alice -> Bob: Session initiation');
      
      // Bob responds to Alice's initiation
      final bobResult = await SecureCommunicationFactory.respondToSecureSession(
        localPeerId: 'bob@example.com',
        initiationMessage: initMessage,
        supportedAsymmetric: [CryptoAlgorithm.rsa],
        supportedSymmetric: [CryptoAlgorithm.aes, CryptoAlgorithm.chacha20],
      );

      print('๐Ÿ“ค Bob -> Alice: Session response');
      return bobResult.responseMessage;
    },
  );

  print('โœ… Secure session established!');
  print('๐Ÿ”‘ Key exchange: ${aliceSession.negotiationResult.keyExchange}');
  print('๐Ÿ” Symmetric cipher: ${aliceSession.negotiationResult.symmetric}');
  print('๐Ÿ” Asymmetric cipher: ${aliceSession.negotiationResult.asymmetric}');
  print('๐Ÿ•’ Session established: ${aliceSession.establishedAt}');
  print('๐Ÿ“ Shared secret length: ${aliceSession.sharedSecret.length} chars\n');

  // Test secure communication
  final messages = [
    '๐Ÿš€ Mission critical data',
    '๐Ÿ’Ž Valuable cryptocurrency keys',
    '๐Ÿฅ Medical records - patient #12345',
    '๐Ÿ“‹ Financial transaction: \$50,000 transfer',
  ];

  for (final message in messages) {
    final encrypted = aliceSession.encryptData(utf8.encode(message));
    final decrypted = aliceSession.decryptData(encrypted);
    final result = utf8.decode(decrypted);
    
    print('๐Ÿ”’ Encrypted & Decrypted: ${result == message ? 'โœ…' : 'โŒ'} "$message"');
  }
}

Low-Level ECDH Key Exchange #

import 'package:cryptdart/cryptdart.dart';

void main() async {
  print('๐Ÿ” Manual ECDH Key Exchange Demo\n');

  // Alice generates her ECDH key pair
  final aliceKeyPair = await ECDHKeyExchange.generateKeyPair(
    curve: ECCKeyUtils.secp256r1,
  );
  
  final aliceECDH = ECDHKeyExchange((
    parent: (
      algorithm: KeyExchangeAlgorithm.ecdh,
      expirationDate: DateTime.now().add(Duration(hours: 1)),
      expirationTimes: null,
    ),
    publicKey: aliceKeyPair['publicKey']!,
    privateKey: aliceKeyPair['privateKey']!,
    curve: ECCKeyUtils.secp256r1,
  ));

  // Bob generates his ECDH key pair
  final bobKeyPair = await ECDHKeyExchange.generateKeyPair(
    curve: ECCKeyUtils.secp256r1,
  );
  
  final bobECDH = ECDHKeyExchange((
    parent: (
      algorithm: KeyExchangeAlgorithm.ecdh,
      expirationDate: DateTime.now().add(Duration(hours: 1)),
      expirationTimes: null,
    ),
    publicKey: bobKeyPair['publicKey']!,
    privateKey: bobKeyPair['privateKey']!,
    curve: ECCKeyUtils.secp256r1,
  ));

  print('๐Ÿ‘ฉ Alice public key: ${aliceECDH.getPublicKey().substring(0, 50)}...');
  print('๐Ÿ‘จ Bob public key: ${bobECDH.getPublicKey().substring(0, 50)}...');

  // Both parties generate the same shared secret
  final aliceSharedSecret = await aliceECDH.generateSharedSecret(
    bobECDH.getPublicKey(),
  );
  
  final bobSharedSecret = await bobECDH.generateSharedSecret(
    aliceECDH.getPublicKey(),
  );

  print('\n๐Ÿ”‘ Alice shared secret: ${aliceSharedSecret.substring(0, 20)}...');
  print('๐Ÿ”‘ Bob shared secret: ${bobSharedSecret.substring(0, 20)}...');
  print('โœ… Secrets match: ${aliceSharedSecret == bobSharedSecret}');
  print('๐Ÿ“ Secret length: ${aliceSharedSecret.length} hex characters');
}

๐Ÿ—๏ธ Architecture Overview #

CryptDart follows a clean, three-layer architecture:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚           Interfaces                โ”‚  Abstract contracts defining operations
โ”‚  ICipher, ISymmetric, IAsymmetric   โ”‚  
โ”‚  ISign, IKeyExchange                โ”‚  
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                    โ”‚
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚       Partial Implementations       โ”‚  Base classes with shared logic
โ”‚  SymmetricCipher, AsymmetricCipher  โ”‚  
โ”‚  CipherBase, SignBase               โ”‚  
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                    โ”‚
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚      Concrete Implementations       โ”‚  Algorithm-specific implementations
โ”‚  AESCipher, RSACipher, HMACSign     โ”‚  
โ”‚  ECDHKeyExchange, ChaCha20Cipher    โ”‚  
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Directory Structure #

lib/
โ”œโ”€โ”€ interfaces/              # Abstract contracts
โ”‚   โ”œโ”€โ”€ i_cipher.dart       # Base cipher interface
โ”‚   โ”œโ”€โ”€ i_sign.dart         # Signature interface  
โ”‚   โ””โ”€โ”€ key_exchange/       # Key exchange interfaces
โ”œโ”€โ”€ implementations/
โ”‚   โ”œโ”€โ”€ partial/            # Base classes with shared logic
โ”‚   โ”œโ”€โ”€ symmetric/          # AES, ChaCha20, DES implementations
โ”‚   โ”œโ”€โ”€ asymmetric/         # RSA implementations
โ”‚   โ”œโ”€โ”€ signed_based/       # HMAC, RSA signatures
โ”‚   โ”œโ”€โ”€ key_exchange/       # ECDH implementation
โ”‚   โ”œโ”€โ”€ session/            # Secure session management
โ”‚   โ””โ”€โ”€ handlers/           # High-level handler classes
โ”œโ”€โ”€ types/                  # Enums and type definitions
โ””โ”€โ”€ utils/                  # Centralized utilities (NO duplication!)

๐Ÿ”ง Advanced Usage #

Custom Algorithm Selection #

import 'package:cryptdart/cryptdart.dart';

void main() async {
  // Create session with specific algorithm preferences
  final customSession = await SecureCommunicationFactory.initiateSecureSession(
    localPeerId: 'secure-app-v2.1',
    supportedAsymmetric: [CryptoAlgorithm.rsa],
    supportedSymmetric: [CryptoAlgorithm.chacha20], // Only ChaCha20
    preferredKeyExchange: KeyExchangeAlgorithm.ecdh, // Prefer ECDH
    sendToRemote: (message) async {
      // Implement your network/IPC communication here
      return await sendToRemotePeer(message);
    },
  );

  print('Selected algorithms:');
  print('  Key Exchange: ${customSession.negotiationResult.keyExchange}');
  print('  Symmetric: ${customSession.negotiationResult.symmetric}');
  print('  Asymmetric: ${customSession.negotiationResult.asymmetric}');
}

Future<Map<String, dynamic>> sendToRemotePeer(Map<String, dynamic> message) async {
  // Your implementation here
  throw UnimplementedError('Implement your communication layer');
}

HMAC Digital Signatures #

import 'package:cryptdart/cryptdart.dart';

void main() async {
  // Generate HMAC key
  final hmacKey = HMACSign.generateKey();
  
  final hmacSign = HMACSign((
    parent: (
      key: hmacKey,
      parent: (
        parent: (
          algorithm: CryptoAlgorithm.hmac,
          expirationDate: DateTime.now().add(Duration(days: 7)),
          expirationTimes: 1000, // Limit to 1000 uses
        ),
      ),
    ),
  ));

  final document = 'Important contract terms and conditions...';
  final signature = await hmacSign.sign(document.codeUnits);
  final isValid = await hmacSign.verifyHMAC(document.codeUnits, signature);
  
  print('Document signed with HMAC: ${isValid ? 'โœ…' : 'โŒ'}');
  print('Remaining uses: ${hmacSign.expirationTimes}');
}

ChaCha20 Stream Cipher #

import 'package:cryptdart/cryptdart.dart';
import 'dart:typed_data';

void main() async {
  // Generate ChaCha20 key and nonce
  final key = ChaCha20Cipher.generateKey();
  final nonce = Uint8List.fromList(List<int>.generate(8, (i) => i * 2));
  
  final cipher = ChaCha20Cipher((
    nonce: nonce,
    parent: (
      key: key,
      parent: (
        parent: (
          algorithm: CryptoAlgorithm.chacha20,
          expirationDate: DateTime.now().add(Duration(hours: 2)),
          expirationTimes: null,
        ),
      ),
    ),
  ));

  final streamData = List<int>.generate(1024, (i) => i % 256); // 1KB data
  final encrypted = cipher.encrypt(streamData);
  final decrypted = cipher.decrypt(encrypted);
  
  print('ChaCha20 stream cipher: ${listEquals(streamData, decrypted) ? 'โœ…' : 'โŒ'}');
  print('Processed ${streamData.length} bytes');
}

bool listEquals<T>(List<T> a, List<T> b) {
  if (a.length != b.length) return false;
  for (int i = 0; i < a.length; i++) {
    if (a[i] != b[i]) return false;
  }
  return true;
}

๐Ÿงช Testing #

Run the comprehensive test suite:

# Run all tests
dart test

# Run specific test suites
dart test test/ecdh_key_exchange_test.dart    # ECDH key exchange tests
dart test test/secure_session_test.dart       # Secure session tests  
dart test test/symmetric_cipher_test.dart     # Symmetric encryption tests
dart test test/asymmetric_cipher_test.dart    # Asymmetric encryption tests
dart test test/key_generation_test.dart       # Key generation tests

# Run tests with coverage
dart test --coverage=coverage
dart pub global activate coverage
dart pub global run coverage:format_coverage --lcov --in=coverage --out=coverage.lcov --packages=.packages --report-on=lib

Test Coverage #

  • โœ… ECDH Key Exchange: 14 comprehensive tests
  • โœ… Secure Sessions: 3 integration tests
  • โœ… Symmetric Ciphers: AES, ChaCha20, DES encryption/decryption
  • โœ… Asymmetric Ciphers: RSA encryption and signatures
  • โœ… Key Generation: All algorithm key generation tests
  • โœ… Error Handling: Invalid inputs, expiration, format errors
  • โœ… Integration: End-to-end secure communication flows

๐Ÿ“š API Reference #

Core Interfaces #

/// Base cipher interface with expiration support
abstract class ICipher extends IExpiration {
  CryptoAlgorithm get algorithm;
  List<int> encrypt(List<int> data);
  List<int> decrypt(List<int> encryptedData);
}

/// Key exchange protocol interface
abstract class IKeyExchange extends IBaseExpiration {
  KeyExchangeAlgorithm get algorithm;
  String get publicKey;
  Future<String> generateSharedSecret(String otherPublicKey);
}

/// Digital signature interface
abstract class ISign extends IBaseExpiration {
  CryptoAlgorithm get algorithm;
  Future<List<int>> sign(List<int> data);
  Future<bool> verifySignature(List<int> data, List<int> signature);
}

Supported Algorithms #

Symmetric Encryption

  • AES: Advanced Encryption Standard (256-bit keys)
  • ChaCha20: Modern stream cipher (256-bit keys + 64-bit nonce)
  • DES: Data Encryption Standard (192-bit keys, legacy)

Asymmetric Encryption

  • RSA: Rivest-Shamir-Adleman (2048, 3072, 4096-bit keys)

Key Exchange

  • ECDH: Elliptic Curve Diffie-Hellman
    • Curves: secp256r1, secp384r1, secp521r1

Digital Signatures

  • HMAC: Hash-based Message Authentication Code
  • RSA Signatures: RSA with SHA-256
  • ECDSA: Elliptic Curve Digital Signature Algorithm (planned)

๐Ÿ›ก๏ธ Security Best Practices #

1. Key Management #

  • โœ… Generate keys using cryptographically secure random number generators
  • โœ… Use appropriate key sizes (AES-256, RSA-2048+, ECDH-256+)
  • โœ… Implement proper key expiration and rotation policies
  • โœ… Never hardcode keys in source code

2. Session Security #

  • โœ… Use ephemeral keys for forward secrecy
  • โœ… Implement proper algorithm negotiation
  • โœ… Validate all inputs and handle errors securely
  • โœ… Use authenticated encryption when possible

3. Algorithm Selection #

  • ๐Ÿฅ‡ Recommended: ChaCha20 + ECDH + RSA signatures
  • ๐Ÿฅˆ Good: AES + ECDH + RSA signatures
  • โš ๏ธ Legacy: DES (avoid in new applications)

๐Ÿค Contributing #

We welcome contributions! Please see our Contributing Guidelines for details.

Development Setup #

# Clone the repository
git clone https://github.com/elguala9/CryptDart.git
cd CryptDart

# Install dependencies
dart pub get

# Run tests
dart test

# Run analysis
dart analyze

๐Ÿ“„ License #

This project is licensed under the GNU Lesser General Public License v3.0 (LGPL-3.0).

What this means:

  • โœ… You can use CryptDart in commercial applications
  • โœ… You can modify CryptDart for your needs
  • โœ… You can distribute applications using CryptDart
  • โ„น๏ธ If you modify CryptDart itself, you must make those modifications available under LGPL-3.0
  • โ„น๏ธ You must include the LGPL-3.0 license notice

See the LICENSE file for complete details.


Built with โค๏ธ for the Dart & Flutter community

Secure by design, easy by choice. ๐Ÿ”

0
likes
0
points
37
downloads

Publisher

unverified uploader

Weekly Downloads

CryptDart is a unified cryptography library for Dart, providing easy-to-use interfaces for symmetric and asymmetric encryption, digital signatures (including ECDSA), and key management. Built on PointyCastle and BasicUtils, LGPL-3.0.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

barrel_files_annotation, basic_utils, crypto, meta, path, pointycastle

More

Packages that depend on cryptdart