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

Verify the RSA Signature of a SHA256 Hash

See more RSA Examples

Demonstrates how to verify an RSA signature of a SHA256 hash.

Chilkat Dart Downloads

Dart
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.

  // Let's say you have a file containing the 32-bytes of a SHA256 hash,
  // and a file that is an RSA signature of those 32 bytes.
  // Here's how you verify using the RSA public key found in a PEM.

  final pubKey = CkPublicKey();
  try {
    pubKey.loadFromFile('rsaPubKey.pem');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final rsa = CkRsa();

  // Get the public key.
  try {
    rsa.usePublicKey(pubKey);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Get the 32-byte SHA256 hash.
  final bdHash = CkBinData();
  try {
    bdHash.loadFile('myHash.sha256');
  } on ChilkatException {
    print('Failed to load SHA256 hash.');
    return;
  }

  // Get the RSA signature to be validated.
  final bdSig = CkBinData();
  try {
    bdSig.loadFile('mySig.sig');
  } on ChilkatException {
    print('Failed to load RSA signature.');
    return;
  }

  // Verify the signature against the SHA256 hash.
  final enc = 'base64';
  rsa.encodingMode = enc;
  try {
    rsa.verifyHashENC(bdHash.getEncoded(enc), 'sha256', bdSig.getEncoded(enc));
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('Signature validated.');
}