Swift Requires Chilkat v11.7.0+
Swift
Replace the Authenticode Signature of an Already Signed File
See more Code Signing Examples
Demonstrates how to sign a file that already has an Authenticode signature, for example to sign it with a renewed code signing certificate. Starting in Chilkat v11.7.0, AddSignature removes the existing signature and replaces it with the new one. Before v11.7.0, signing an already signed EXE or DLL added a second signature entry instead.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.
// Starting in Chilkat v11.7.0, AddSignature replaces the signature of a file that is already signed.
// (Before v11.7.0, signing an already signed EXE or DLL added a second signature entry
// instead of replacing the first.)
//
// This applies to every supported file type: EXE, DLL, MSI, PowerShell, and VBScript.
var path: String? = "C:/someDir/MyApp.exe"
let codeSign = CkoCodeSign()!
let sigInfo = CkoJsonObject()!
let signerCert = CkoCert()!
// Find out who signed the file.
var valid: Bool = codeSign.verifySignature(path: path, sigInfo: sigInfo)
if valid == true {
codeSign.getSignerCert(cert: signerCert)
print("currently signed by: \(signerCert.subjectCN!)")
print("certificate valid until: \(signerCert.validToStr!)")
}
else {
print("The file is not signed, or its signature is not valid.")
}
// Load the certificate to sign with. It must have access to its private key.
let cert = CkoCert()!
success = cert.loadPfxFile(path: "C:/someDir/myNewCodeSigningCert.pfx", password: "pfx password")
if success == false {
print("\(cert.lastErrorText!)")
return
}
// Sign with SHA256, and ask a timestamp authority for a timestamp. A timestamp lets the
// signature continue to validate after the code signing certificate has expired.
//
// {
// "hashAlg": "sha256",
// "timestampToken": {
// "enabled": true,
// "tsaUrl": "http://timestamp.digicert.com",
// "requestTsaCert": true,
// "hashAlg": "sha256"
// }
// }
let json = CkoJsonObject()!
json.updateString(jsonPath: "hashAlg", value: "sha256")
json.updateBool(jsonPath: "timestampToken.enabled", value: true)
json.updateString(jsonPath: "timestampToken.tsaUrl", value: "http://timestamp.digicert.com")
json.updateBool(jsonPath: "timestampToken.requestTsaCert", value: true)
json.updateString(jsonPath: "timestampToken.hashAlg", value: "sha256")
// Sign the file. It is not necessary to call RemoveSignature first:
// the existing signature is removed and the new signature takes its place.
success = codeSign.addSignature(path: path, cert: cert, options: json)
if success == false {
print("\(codeSign.lastErrorText!)")
return
}
// The file now has one signature, made with the new certificate.
valid = codeSign.verifySignature(path: path, sigInfo: sigInfo)
if valid == false {
print("\(codeSign.lastErrorText!)")
return
}
success = codeSign.getSignerCert(cert: signerCert)
if success == false {
print("\(codeSign.lastErrorText!)")
return
}
print("now signed by: \(signerCert.subjectCN!)")
print("certificate valid until: \(signerCert.validToStr!)")
}