crosspki 1.1.1 crosspki: ^1.1.1 copied to clipboard
Flutter plugin for handling X509 certificates and key pairs from native platform key stores.
import 'dart:convert';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:crosspki/crosspki.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
runCrossPkiApis();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> runCrossPkiApis() async {
try {
print('LISTING CERTIFICATES');
var certs = await CrossPki.listCertificatesWithKey();
print('Certs count: ${certs.length}');
print('IMPORTING PKCS#12');
final p12Content = base64Decode(
'MIACAQMwgAYJKoZIhvcNAQcBoIAkgASCA+gwgDCABgkqhkiG9w0BBwGggCSABIID6DCCBW8wggVrBgsqhkiG9w0BDAoBAqCCBPowggT2MCgGCiqGSIb3DQEMAQMwGgQUSRKNXrxw7DWmYm7v2lWpTGMct4wCAgQABIIEyOJLkFDlyhRki9N2vfd9BrBemKPr5OP6b1eQadDxwt/SKXzwpKv9DDmHYq904EeBFXKjIdCliRq6F14uht63EiYZYlGQp7wNNe8JS547q3jJqYTTZ4GMQ3a5L1tJqfZrU+mJrDpphXDAeQOPpSUtyUlYKLcn8IBc5hO/7ZpsPDFkjEYTalw4xDyyZyariUhtY3POVGSNBrVTsjZVK4iOD8NLaMyVSGI45waCA/VXuffwzBxEkgEiMw5N4uHq/WkLsKueDf8D5+yQRhDAgEENnXk4FQGPDcLiL7OpBakn9Hc0iTkO+7C0ow1P1KdfcEZfjfJN2xF/jCqHfR77yX+HUkyd9guLX5pfPsoMcD6Uwl7sOzvPaDdTU6cfiu/nLkl8JS9olBTLyb8yvYwCqRbKI4wmgv3DxOHy6AiGJoHfcWGu55j8chONOjMVf7XUWnibvFmpRaauSs7ffrA1gKv59W0P9HmpQmwIZ/Esfgz4Xye2nS9zX4AwitaT36Fuadwy6qMsJ8i04fi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gskc15JKUYgyfxAVqCKpO2oVmcGc/8yvMGdZTigTNS9hmVrsvsA5KuUVTlS87WS9DktpGtzHFbs2wnSE6IbWAdNRPjLHv8Gr9ZdQyDJCnzX3m2YEcvmPXQ5Uxzo7Quk84CgQCuEWoEjRpsdkrn2rUTB8QSCAYWfQJdFd1WoOCBCc6VtNVxRDr4Kl0aAcO4nphwPn9adL59K1wGUYo2Of3MGqZ79VJyEHhx7v620MS5H/en+nL64FcTkOUp4IauZtXDw38BGV+4IEd3vPbkv8fy1Fqh9H6aRDaWYDLqzZcJ6fzPFI2bk1YNxBun5Gx2y7vIa+YQRhLtaBiAAiPa8wNfG9lzKK1g89c0C2cawV6bwHt5dRGcI+q79jlakh0+nHFOlfXcxqws8ku6JH0atP/kpMlKjCBNBGf4srEd9xOgxjSc8sMpdgOL+rivIv1McflR+Y+xbUTKRX41XZZaO/Kkz8IdnVyIE6eCOW2bFdqkTaYBnaM0+qRa5Ebk8CXvTVjRnphIFvnRLifjoyTh8qUmRYNdGSztM+irzmxRywnA1hL09fhWY+afaJazSryYq1D/DP0OHqhjiq/ej0S2sQ0GUoe9l/3aQz8lCie6tFvmN8L4NwRyOv+sptxGa8QxW281ybElJo+WF2lPDxQw6JAAAAAAAAAAAAAAAAAAAAAAAADA9MCEwCQYFKw4DAhoFAAQUc/Z37zPydcC06MSdcloqHZaZSAwEFGZA4R1dD6lwzAVQRNIvoney9tWKAgIEAAAA');
await CrossPki.importPkcs12(p12Content, "1234");
certs = await CrossPki.listCertificatesWithKey();
print('Certs count: ${certs.length}');
final userCertificate = certs.first;
print('Imported: ${userCertificate.subjectName}');
print('CPF: ${userCertificate.pkiBrazil.cpfFormatted}');
print('SIGN DATA TEST');
var sig = await CrossPki.signData(userCertificate.thumbprint,
DigestAlgorithm.sha256, base64Decode('SGVsbG8sIFdvcmxkIQ=='));
print('Signature: ${base64Encode(sig.toList())}');
print('SIGN HASH TEST');
sig = await CrossPki.signHash(
userCertificate.thumbprint,
DigestAlgorithm.sha256,
base64Decode('3/1gIbsr1bCvZ2KQgJ7DpTGR3YHH9wpLKGiKNiGCmG8='));
print('Signature: ${base64Encode(sig.toList())}');
await CrossPki.removeCertificate(certs[0].thumbprint);
print('Removed');
certs = await CrossPki.listCertificatesWithKey();
print('Certs count: ${certs.length}');
print('GENERATING RSA KEY PAIR...');
var keyPair = await CrossPki.generateRsaKeyPair(2048);
print('keyId: ${keyPair.keyId}');
print(
'publicKey ${keyPair.algorithm}: ${base64Encode(keyPair.publicKeyEncoded.toList())}');
print('GENERATING CSR...');
var csr = await CrossPki.generateCsr(keyPair);
print('CSR: ${base64Encode(csr.toList())}');
print('DONE');
} on CrossPkiException catch (ex) {
print('ERROR: $ex');
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Cross PKI Plugin example app'),
),
body: const Center(
child: Text('Running Cross PKI API'),
),
),
);
}
}