Sample code for 30+ languages & platforms
Xbase++ 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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oPkey
LOCAL oRsa
LOCAL oBdFileData
LOCAL oBdSig

nSuccess := 0

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

oPkey := CreateObject("Chilkat.PrivateKey")

//  Load the private key from an PEM file:
nSuccess := oPkey:LoadPemFile("private.pem")
IF (nSuccess == 0)
    ? oPkey:LastErrorText
    oPkey:destroy()
    RETURN
ENDIF

oRsa := CreateObject("Chilkat.Rsa")

//  Import the private key into the RSA component:
nSuccess := oRsa:UsePrivateKey(oPkey)
IF (nSuccess == 0)
    ? oRsa:LastErrorText
    oPkey:destroy()
    oRsa:destroy()
    RETURN
ENDIF

//  OpenSSL uses big-endian.
oRsa:LittleEndian := 0

//  Load the file to be signed.
oBdFileData := CreateObject("Chilkat.BinData")
nSuccess := oBdFileData:LoadFile("in.dat")

oBdSig := CreateObject("Chilkat.BinData")
nSuccess := oRsa:SignBd(oBdFileData, "sha256", oBdSig)
IF (nSuccess == 0)
    ? oRsa:LastErrorText
    oPkey:destroy()
    oRsa:destroy()
    oBdFileData:destroy()
    oBdSig:destroy()
    RETURN
ENDIF

//  Save the binary signature to a file.
nSuccess := oBdSig:WriteFile("signature.sig")
IF (nSuccess != 1)
    ? "Failed to write signature.sig."
    oPkey:destroy()
    oRsa:destroy()
    oBdFileData:destroy()
    oBdSig:destroy()
    RETURN
ENDIF

? "Success."

oPkey:destroy()
oRsa:destroy()
oBdFileData:destroy()
oBdSig:destroy()