Skip to main content

post_quantum_crypto 1.0.0 copy "post_quantum_crypto: ^1.0.0" to clipboard
post_quantum_crypto: ^1.0.0 copied to clipboard

High-level post-quantum cryptography for Dart: ML-KEM envelopes, multi-recipient and streaming encryption, ML-DSA signatures, X-Wing hybrid key establishment, Argon2id-protected keys and key managemen [...]

example/post_quantum_crypto_example.dart

// Quick tour of post_quantum_crypto. Run with: dart run example/post_quantum_crypto_example.dart
import 'dart:convert';

import 'package:post_quantum_crypto/post_quantum_crypto.dart';

void main() {
  final engine = PostQuantumCryptoEngine();

  // --- Encryption (ML-KEM-768 + HPKE + AES-256-GCM) -------------------------
  final alice = engine.generateEncryptionKeyPair();
  final bob = engine.generateEncryptionKeyPair(algorithm: KemAlgorithm.xWing);

  final envelope = engine.encrypt(
    utf8.encode('Quarterly numbers: confidential'),
    recipients: [alice.publicKey, bob.publicKey],
    context: {'tenant': 'acme', 'record': 'report-2026-q3'},
  );
  final wire = envelope.toBytes(); // canonical binary form
  print('envelope: ${wire.length} bytes, recipients: ${envelope.recipients}');

  final plaintext = engine.decryptBytes(
    wire,
    privateKey: bob.privateKey,
    requiredContext: {'tenant': 'acme'},
  );
  print('bob reads: ${utf8.decode(plaintext)}');

  // --- Signatures (ML-DSA-65) ----------------------------------------------
  final signer = engine.generateSigningKeyPair();
  final message = utf8.encode('release 1.0.0');
  final signature = engine.sign(
    message,
    privateKey: signer.privateKey,
    context: utf8.encode('example/releases'),
  );
  final valid = engine.verify(
    message,
    signature: signature,
    publicKey: signer.publicKey,
    context: utf8.encode('example/releases'),
  );
  print('signature valid: $valid (${signature.length} bytes)');

  // A valid signature is not the same as a trusted signer.
  final trust = TrustStore([signer.publicKey]);
  final detached = engine.signDetached(
    message,
    privateKey: signer.privateKey,
    metadata: {'name': 'app.tar.gz'},
  );
  final verified = engine.verifyDetached(
    message,
    signature: detached,
    publicKey: trust.lookup(detached.signerKeyId)!,
  );
  print('trust decision: ${trust.evaluate(verified)}');

  // --- Key export -----------------------------------------------------------
  print(engine.exportPublicKeyPem(signer.publicKey).split('\n').first);
  print('key id: ${alice.keyId.fingerprint}');

  // --- Hybrid key establishment (X-Wing) -----------------------------------
  const transcript = HybridTranscript(
    protocol: 'example/v1',
    initiatorId: 'client',
    responderId: 'server',
  );
  final init = engine.establishHybridKey(bob.publicKey, transcript: transcript);
  final serverKey = engine.acceptHybridKey(
    init.message,
    privateKey: bob.privateKey,
    transcript: transcript,
  );
  print(
    'hybrid keys match: '
    '${TextEncoding.toHex(serverKey) == TextEncoding.toHex(init.sessionKey)}',
  );
}
1
likes
0
points
1.3k
downloads

Publisher

unverified uploader

Weekly Downloads

High-level post-quantum cryptography for Dart: ML-KEM envelopes, multi-recipient and streaming encryption, ML-DSA signatures, X-Wing hybrid key establishment, Argon2id-protected keys and key management. Pure Dart, built on pqcrypto and PointyCastle behind swappable provider interfaces.

Repository (GitHub)
View/report issues

Topics

#cryptography #post-quantum #encryption #signatures #security

License

unknown (license)

Dependencies

cryptography, meta, pointycastle, pqcrypto

More

Packages that depend on post_quantum_crypto