Sample code for 30+ languages & platforms
Dart Requires Chilkat v11.0.0+

RSA Hash Binary Data and Sign (and Verify)

See more RSA Examples

Demonstrates how to sign the hash of binary data. Also demonstrates how to verify the RSA signature.

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // This example requires the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  // Load an RSA private key for signing.
  final privKey = CkPrivateKey();
  try {
    privKey.loadEncryptedPemFile('qa_data/pem/rsa_passwd.pem', 'passwd');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final rsa = CkRsa();
  rsa.usePrivateKey(privKey);

  // We have some binary data (in hex) to sign
  final originalData = '0102030405060708090A';
  final bdData = CkBinData();
  bdData.appendEncoded(originalData, 'hex');

  // Hash (SHA-256) and sign the hash:
  final bdSignature = CkBinData();
  try {
    rsa.signBd(bdData, 'sha256', bdSignature);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Show the RSA signature in base64
  print(bdSignature.getEncoded('base64'));

  // ------------------------------------------
  // Get the public key from the private key
  final pubKey = CkPublicKey();
  privKey.toPublicKey(pubKey);

  // Verify the signature..
  final rsa2 = CkRsa();
  rsa2.usePublicKey(pubKey);

  var bVerified = true;
  try {
    rsa2.verifyBd(bdData, 'sha256', bdSignature);
  } on ChilkatException {
    bVerified = false;
  }
  print('signature verified: $bVerified');
}