flutter_ever_crypto 0.1.1
flutter_ever_crypto: ^0.1.1 copied to clipboard
Flutter plugin for Ever Crypto - XChaCha20Poly1305 and Kyber1024 post-quantum cryptography
example/lib/main.dart
import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_ever_crypto/flutter_ever_crypto.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _status = 'Ready';
String _result = '';
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Ever Crypto Example'),
backgroundColor: Colors.deepPurple,
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
Text(
'Status: $_status',
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: _testXChaChaEncryption,
child: const Text('Test XChaCha20Poly1305'),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: _testKyberKeyExchange,
child: const Text('Test Kyber1024 Key Exchange'),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: _testHybridEncryption,
child: const Text('Test Hybrid Encryption'),
),
const SizedBox(height: 20),
Expanded(
child: Container(
width: double.infinity,
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(8),
),
child: SingleChildScrollView(
child: Text(
_result,
style: const TextStyle(fontFamily: 'monospace'),
),
),
),
),
],
),
),
),
);
}
void _testXChaChaEncryption() async {
setState(() {
_status = 'Testing XChaCha20Poly1305...';
_result = '';
});
try {
// Generate key and nonce
final key = EverCrypto.generateXChaChaKey();
final nonce = EverCrypto.generateXChaChaNonce();
// Test message
final message = 'Hello, World! This is a secret message.';
final plaintext = Uint8List.fromList(utf8.encode(message));
final aad = Uint8List.fromList(
utf8.encode('Additional authenticated data'),
);
// Encrypt
final ciphertext = EverCrypto.xchachaEncrypt(
key,
nonce,
plaintext,
aad: aad,
);
// Decrypt
final decrypted = EverCrypto.xchachaDecrypt(
key,
nonce,
ciphertext,
aad: aad,
);
final decryptedMessage = utf8.decode(decrypted);
setState(() {
_status = 'XChaCha20Poly1305 test completed successfully!';
_result =
'''
XChaCha20Poly1305 Encryption Test:
Original message: $message
Key size: ${key.length} bytes
Nonce size: ${nonce.length} bytes
Ciphertext size: ${ciphertext.length} bytes
Decrypted message: $decryptedMessage
Test result: ${message == decryptedMessage ? 'PASSED ✅' : 'FAILED ❌'}
''';
});
} catch (e) {
setState(() {
_status = 'XChaCha20Poly1305 test failed';
_result = 'Error: $e';
});
}
}
void _testKyberKeyExchange() async {
setState(() {
_status = 'Testing Kyber1024 key exchange...';
_result = '';
});
try {
// Generate Alice's key pair
final aliceKeyPair = EverCrypto.generateKyberKeyPair();
// Bob encapsulates a shared secret
final bobResult = EverCrypto.kyberEncapsulate(aliceKeyPair.publicKey);
// Alice decapsulates the shared secret
final aliceSharedSecret = EverCrypto.kyberDecapsulate(
bobResult.ciphertext,
aliceKeyPair.secretKey,
);
// Compare shared secrets
final secretsMatch = _uint8ListsEqual(
bobResult.sharedSecret,
aliceSharedSecret,
);
setState(() {
_status = 'Kyber1024 test completed!';
_result =
'''
Kyber1024 Key Exchange Test:
Alice's public key size: ${aliceKeyPair.publicKey.length} bytes
Alice's secret key size: ${aliceKeyPair.secretKey.length} bytes
Encapsulated ciphertext size: ${bobResult.ciphertext.length} bytes
Bob's shared secret size: ${bobResult.sharedSecret.length} bytes
Alice's shared secret size: ${aliceSharedSecret.length} bytes
Shared secrets match: ${secretsMatch ? 'PASSED ✅' : 'FAILED ❌'}
''';
});
} catch (e) {
setState(() {
_status = 'Kyber1024 test failed';
_result = 'Error: $e';
});
}
}
void _testHybridEncryption() async {
setState(() {
_status = 'Testing hybrid encryption...';
_result = '';
});
try {
// Step 1: Kyber1024 key establishment
final aliceKeyPair = EverCrypto.generateKyberKeyPair();
final bobResult = EverCrypto.kyberEncapsulate(aliceKeyPair.publicKey);
// Step 2: Use shared secret as XChaCha key
final xchachaKey = bobResult.sharedSecret.sublist(
0,
32,
); // Use first 32 bytes
final nonce = EverCrypto.generateXChaChaNonce();
// Step 3: Encrypt data
final message =
'This is a secret message using hybrid post-quantum encryption!';
final plaintext = Uint8List.fromList(utf8.encode(message));
final ciphertext = EverCrypto.xchachaEncrypt(
xchachaKey,
nonce,
plaintext,
);
// Decryption process:
// Step 1: Alice decapsulates the shared secret
final aliceSharedSecret = EverCrypto.kyberDecapsulate(
bobResult.ciphertext,
aliceKeyPair.secretKey,
);
// Step 2: Use shared secret as XChaCha key
final aliceXchachaKey = aliceSharedSecret.sublist(0, 32);
// Step 3: Decrypt the message
final decrypted = EverCrypto.xchachaDecrypt(
aliceXchachaKey,
nonce,
ciphertext,
);
final decryptedMessage = utf8.decode(decrypted);
setState(() {
_status = 'Hybrid encryption test completed!';
_result =
'''
Hybrid Encryption Test (Kyber1024 + XChaCha20Poly1305):
Original message: $message
Kyber ciphertext size: ${bobResult.ciphertext.length} bytes
XChaCha nonce size: ${nonce.length} bytes
XChaCha ciphertext size: ${ciphertext.length} bytes
Decrypted message: $decryptedMessage
Test result: ${message == decryptedMessage ? 'PASSED ✅' : 'FAILED ❌'}
This demonstrates post-quantum secure messaging!
''';
});
} catch (e) {
setState(() {
_status = 'Hybrid encryption test failed';
_result = 'Error: $e';
});
}
}
bool _uint8ListsEqual(Uint8List a, Uint8List 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;
}
}