Swift
Swift
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 Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let crypt = CkoCrypt2()!
let certStore = CkoCertStore()!
// Load a PFX file into a certificate store object.
success = certStore.loadPfxFile(path: "myPfx.pfx", password: "pfxPassword")
if success != true {
print("\(certStore.lastErrorText!)")
return
}
// 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).
let jsonCN = CkoJsonObject()!
jsonCN.updateString(jsonPath: "CN", value: "myCert")
let cert = CkoCert()!
success = certStore.findCert(json: jsonCN, cert: cert)
if success == false {
print("\(certStore.lastErrorText!)")
return
}
// Tell the crypt object to use the certificate for signing:
success = crypt.setSigningCert(cert: 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.
var inFile: String? = "test.txt"
var outFile: String? = "testp7m"
success = crypt.createP7M(inPath: inFile, p7mPath: outFile)
if success != true {
print("\(crypt.lastErrorText!)")
return
}
print("Success!")
}