Swift
Swift
RSA Sign Using Private Key from .pfx/.p12 to Base64 Signature
See more RSA Examples
Demonstrates how to RSA sign something using a private key loaded from a .pfx/.p12. The RSA signature is returned in Base64 encoded format.Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let rsa = CkoRsa()!
// Load the .pfx/.p12
let pfx = CkoPfx()!
success = pfx.loadFile(path: "qa_data/pfx/myKey.p12", password: "myPassword")
if success == false {
print("\(pfx.lastErrorText!)")
return
}
// Get the default private key.
let privKey = CkoPrivateKey()!
success = pfx.privateKey(at: 0, privKey: privKey)
if success == false {
print("\(pfx.lastErrorText!)")
return
}
// Import the private key into the RSA component:
success = rsa.usePrivateKey(privKey: privKey)
if success == false {
print("\(rsa.lastErrorText!)")
return
}
// Get the signature in base64
rsa.encodingMode = "base64"
var strData: String? = "This is the string to be signed."
// Sign the string using the sha256 hash algorithm.
// Other valid choices are "sha384", "sha512", "sha-1", "md2" and "md5".
var base64Sig: String? = rsa.signStringENC(str: strData, hashAlg: "sha256")
print("\(base64Sig!)")
print("Success!")
}