Dart Requires Chilkat v11.0.0+
Dart
Iterate Keys and Certs in PEM
See more PEM Examples
Demonstrates how to access each of the private keys and certs contained within a PEM.Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
final pem = CkPem();
// Load the PEM from a file.
// If the PEM is encrypted, provide a password. Otherwise pass an empty string for the password.
final password = 'myPassword';
try {
pem.loadPemFile('../myPemFiles/myPem.pem', password);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Note: If the app already has the PEM pre-loaded in a string variable, then load it
// by calling LoadPem instead.
final pemContent = '... the PEM contents ...';
pem.loadPem(pemContent, password);
// Check for success as before..
// Iterate over the private keys.
final numPrivateKeys = pem.numPrivateKeys;
var i = 0;
final privKey = CkPrivateKey();
while (i < numPrivateKeys) {
pem.privateKeyAt(i, privKey);
print('Private Key $i is ${privKey.bitLength} in length');
i++;
}
// Iterate over the certificates.
final cert = CkCert();
final numCerts = pem.numCerts;
i = 0;
while (i < numCerts) {
pem.certAt(i, cert);
print('Certificate $i : ${cert.subjectDN}');
i++;
}
}