jc_crypto_hashing 0.0.1
jc_crypto_hashing: ^0.0.1 copied to clipboard
Android JCA backend for cryptographic operations using jnigen.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:typed_data';
import 'package:jc_crypto_hashing/jc_crypto_hashing.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 = 'Initializing...';
final _crypto = CryptoJCA();
@override
void initState() {
super.initState();
_runDemo();
}
Future<void> _runDemo() async {
try {
final data = Uint8List.fromList("Hello from JNI".codeUnits);
final hash = await _crypto.digest(data);
final key = await _crypto.generateSoftwareKey(bits: 256);
final iv = CryptoJCA.generateIv();
final encrypted = await _crypto.aesGcmEncrypt(data, iv, cryptoKey: key);
final decrypted = await _crypto.aesGcmDecrypt(encrypted, iv, cryptoKey: key);
if (!mounted) return;
setState(() {
_status = "JCA Ready!\n\nSHA-256: ${hash.length} bytes\nDecrypted: ${String.fromCharCodes(decrypted)}";
});
} catch (e) {
if (!mounted) return;
setState(() {
_status = "Error: $e";
});
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(useMaterial3: true, colorSchemeSeed: Colors.blue),
home: Scaffold(
appBar: AppBar(
title: const Text('JCA Crypto Example'),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Text(_status, textAlign: TextAlign.center, style: const TextStyle(fontSize: 18)),
),
),
),
);
}
}