pqforge 0.1.0 copy "pqforge: ^0.1.0" to clipboard
pqforge: ^0.1.0 copied to clipboard

A secure, opinionated cryptographic wrapper designed to enforce safe post-quantum and classical composition in Dart/Flutter and Serverpod ecosystems. By bridging pqcrypto and pointycastle, pqforge eli [...]

example/pqforge_example.dart

import 'dart:convert';
import 'dart:typed_data';

import 'package:pqforge/pqforge.dart';

void main() {
  final forge = PqForge(profile: PqForgeProfile.compact);
  final message = Uint8List.fromList(utf8.encode('pqforge example payload'));

  print('== 1. Generate keys ==');
  final bundle = forge.generateKeys(keyId: 'example-bundle');
  print(
    '${bundle.profile.name}: '
    '${bundle.profile.kem.name} + ${bundle.profile.signature.name}',
  );

  print('\n== 2. Sign and verify a document ==');
  final documentSignature = forge.signDocument(
    bundle.signatureKeyPair.secretKey,
    message,
    documentId: 'example-document',
  );
  final documentOk = forge.verifyDocument(
    bundle.signatureKeyPair.publicKey,
    message,
    documentSignature,
    documentId: 'example-document',
  );
  print('document verified: $documentOk');

  print('\n== 3. Encrypt and decrypt a record ==');
  final envelope = forge.encrypt(
    bundle.kemKeyPair.publicKey,
    message,
    aad: PqBytes.utf8Bytes('record:example'),
    metadata: {'recordType': 'demo'},
  );
  final opened = forge.decrypt(
    bundle.kemKeyPair.secretKey,
    envelope,
    aad: PqBytes.utf8Bytes('record:example'),
  );
  print('opened: ${utf8.decode(opened)}');

  print('\n== 4. Binary and JSON envelopes ==');
  final binary = envelope.toBinary();
  final jsonEnvelope = envelope.toJson();
  print('binary bytes: ${binary.length}');
  print('json keys: ${jsonEnvelope.keys.join(', ')}');

  print('\n== 5. Signed encrypted envelope ==');
  final signed = forge.encrypt(
    bundle.kemKeyPair.publicKey,
    message,
    signerSecretKey: bundle.signatureKeyPair.secretKey,
    signerKeyId: 'example-signer',
  );
  final signedOpened = forge.decrypt(
    bundle.kemKeyPair.secretKey,
    signed,
    signerPublicKey: bundle.signatureKeyPair.publicKey,
  );
  print('signed opened: ${utf8.decode(signedOpened)}');

  print('\n== 6. Hybrid session derivation ==');
  final kem = forge.encapsulate(bundle.kemKeyPair.publicKey);
  final classicalSharedSecret = PqBytes.randomBytes(32);
  final transcript = PqBytes.lengthPrefixed([
    PqBytes.utf8Bytes('example/handshake/v1'),
    bundle.kemKeyPair.publicKey,
    kem.ciphertext,
  ]);
  final sessionKey = forge.deriveHybridSessionKey(
    classicalSharedSecret: classicalSharedSecret,
    latticeSharedSecret: kem.sharedSecret,
    deploymentSalt: PqBytes.randomBytes(32),
    transcriptHash: PqBytes.sha256(transcript),
    roleContext: PqBytes.utf8Bytes('client->server'),
  );
  print('hybrid session key: ${sessionKey.length} bytes');
}
1
likes
0
points
134
downloads

Publisher

unverified uploader

Weekly Downloads

A secure, opinionated cryptographic wrapper designed to enforce safe post-quantum and classical composition in Dart/Flutter and Serverpod ecosystems. By bridging pqcrypto and pointycastle, pqforge eliminates common deployment anti-patterns, ensuring type-safe, misuse-resistant implementations of hybrid quantum-safe protocols across the full stack.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

args, cryptography, pointycastle, pqcrypto

More

Packages that depend on pqforge