Sample code for 30+ languages & platforms
Tcl

Create PKCS7 Signed File (.p7m)

See more Encryption Examples

Demonstrates how to sign a file to create a .p7m that contains both the file contents and the signature.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

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

set crypt [new_CkCrypt2]

set certStore [new_CkCertStore]

# Load a PFX file into a certificate store object.
set success [CkCertStore_LoadPfxFile $certStore "myPfx.pfx" "pfxPassword"]
if {$success != 1} then {
    puts [CkCertStore_lastErrorText $certStore]
    delete_CkCrypt2 $crypt
    delete_CkCertStore $certStore
    exit
}

# Get the certificate by subject common name.
# This should be the cert within the PFX that also
# has a private key (also stored within the PFX).
set jsonCN [new_CkJsonObject]

CkJsonObject_UpdateString $jsonCN "CN" "myCert"
set cert [new_CkCert]

set success [CkCertStore_FindCert $certStore $jsonCN $cert]
if {$success == 0} then {
    puts [CkCertStore_lastErrorText $certStore]
    delete_CkCrypt2 $crypt
    delete_CkCertStore $certStore
    delete_CkJsonObject $jsonCN
    delete_CkCert $cert
    exit
}

# Tell the crypt object to use the certificate for signing:
set success [CkCrypt2_SetSigningCert $crypt $cert]

# Sign a file, producing a .p7m as output.
# The input file is unchanged, the test.p7m contains the 
# contents of the input file and the signature.
set inFile "test.txt"
set outFile "testp7m"
set success [CkCrypt2_CreateP7M $crypt $inFile $outFile]
if {$success != 1} then {
    puts [CkCrypt2_lastErrorText $crypt]
    delete_CkCrypt2 $crypt
    delete_CkCertStore $certStore
    delete_CkJsonObject $jsonCN
    delete_CkCert $cert
    exit
}

puts "Success!"

delete_CkCrypt2 $crypt
delete_CkCertStore $certStore
delete_CkJsonObject $jsonCN
delete_CkCert $cert