Sample code for 30+ languages & platforms
Dart Requires Chilkat v10.1.2+

RSA Sign using Private Key of Certificate Type A3 (smart card / token)

See more RSA Examples

Demonstrates RSA signing data using the private key of a certificate type A3 (smart card, token).

Note: This is a Windows-only example.

Chilkat Dart Downloads

Dart
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;
  }

  // Now we're ready to sign..
  final fac = CkFileAccess();

  // Get bytes to be signed..
  final inData = fac.readEntireFile('in.dat');
  final signature = rsa.signBytes(inData, 'SHA-256');
  if (!rsa.lastMethodSuccess) {
    print(rsa.lastErrorText);
    return;
  }

  print('Signature created.');
}