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

Lua
local chilkat = require("chilkat")

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

local pkey = chilkat.PrivateKey.new()

-- Load the private key from an PEM file:
local success = pkey:load_pem_file("private.pem")
if not success then
    print(pkey.last_error_text)
    return
end

local rsa = chilkat.Rsa.new()

-- Import the private key into the RSA component:
success = rsa:use_private_key(pkey)
if not success then
    print(rsa.last_error_text)
    return
end

-- OpenSSL uses big-endian.
rsa.little_endian = false

-- Load the file to be signed.
local bdFileData = chilkat.BinData.new()
success = bdFileData:load_file("in.dat")

local bdSig = chilkat.BinData.new()
success = rsa:sign_bd(bdFileData, "sha256", bdSig)
if not success then
    print(rsa.last_error_text)
    return
end

-- Save the binary signature to a file.
success = bdSig:write_file("signature.sig")
if not success then
    print("Failed to write signature.sig.")
    return
end

print("Success.")