Sample code for 30+ languages & platforms
Lua

Duplicate OpensSSL to Create Signature using Cert and Key Files

See more OpenSSL Examples

This example duplicates the following:
openssl smime –sign -in something.xml -out something.der -signer mycert.crt -inkey cert.key -outform der –nodetach

Note: Although "smime" is the OpenSSL command, it's not actually producing S/MIME. The arguments "-outform der -binary" indicates that the output is binary DER (i.e. the PKCS7 binary signature). The input can be any type of file: XML, PDF, JPG, ... *anything*...

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 crypt = chilkat.Crypt2.new()

-- Load the cert and private key from separate files.
local cert = chilkat.Cert.new()
local success = cert:load_from_file("myCert.crt")
if not success then
    print(cert.last_error_text)
    return
end

local bd = chilkat.BinData.new()
success = bd:load_file("cert.key")
local privkey = chilkat.PrivateKey.new()
-- Load from any format private key.
-- If the file does not need a password, the 2nd arg is ignored.
success = privkey:load_any_format(bd, "password_if_needed")
if not success then
    print(privkey.last_error_text)
    return
end

success = crypt:set_signing_cert2(cert, privkey)
if not success then
    print(crypt.last_error_text)
    return
end

-- Create the opaque signature (PKCS7 binary DER that contains both the signature and original file data).
success = crypt:create_p7_m("qa_data/infile.anything", "qa_output/outfile.der")
if not success then
    print(crypt.last_error_text)
    return
end

print("Success.")