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

Duplicate openssl dgst -sha256 -sign private.pem -out sha256.sig in.dat

See more OpenSSL Examples

Demonstrates how to duplicate this OpenSSL command:
openssl dgst -sha256 -sign private.pem -out sha256.sig in.dat
The in.dat file can contain text or binary data of any type. The OpenSSL command does the following:
  1. Creates a SHA256 digest of the contents of the input file
  2. Signs the SHA256 digest using the private key.

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.

  final pkey = CkPrivateKey();

  // Load the private key from an PEM file:
  try {
    pkey.loadPemFile('private.pem');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final rsa = CkRsa();

  // Import the private key into the RSA component:
  try {
    rsa.usePrivateKey(pkey);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // OpenSSL uses big-endian.
  rsa.littleEndian = false;

  // Load the file to be signed.
  final bdFileData = CkBinData();
  bdFileData.loadFile('in.dat');

  final bdSig = CkBinData();
  try {
    rsa.signBd(bdFileData, 'sha256', bdSig);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Save the binary signature to a file.
  try {
    bdSig.writeFile('signature.sig');
  } on ChilkatException {
    print('Failed to write signature.sig.');
    return;
  }

  print('Success.');
}