Dart Requires Chilkat v10.1.2+
Dart
RSA Sign String using Private Key of Certificate Type A3 (smart card / token)
See more RSA Examples
Demonstrates RSA signing a string using the private key of a certificate type A3 (smart card, token).Note: This is a Windows-only example.
Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// First get the A3 certificate that was installed on the Windows system.
final certStore = CkCertStore();
final thumbprint = '12c1dd8015f3f03f7b1fa619dc24e2493ca8b4b2';
// This is specific to Windows because it is opening the Windows Current-User certificate store.
final bReadOnly = true;
try {
certStore.openCurrentUserStore(bReadOnly);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Find the certificate with the desired thumbprint
// (There are many ways to locate a certificate. This example chooses to find by thumbprint.)
final json = CkJsonObject();
json.updateString('thumbprint', thumbprint);
final cert = CkCert();
try {
certStore.findCert(json, cert);
} on ChilkatException {
print('Failed to find the certificate.');
return;
}
print('Found: ${cert.subjectCN}');
final rsa = CkRsa();
// Provide the cert's private key
final bUsePrivateKey = true;
try {
rsa.setX509Cert(cert, bUsePrivateKey);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Return the RSA signature in base64 encoded form.
rsa.encodingMode = 'base64';
// Sign the utf-8 byte representation of the string.
rsa.charset = 'utf-8';
// You can also choose other hash algorithms, such as SHA-1.
final String sigBase64;
try {
sigBase64 = rsa.signStringENC('text to sign', 'SHA-256');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
print('Base64 signature: $sigBase64');
}