Sample code for 30+ languages & platforms
Swift

RSA Sign using Base64 Private Key

See more RSA Examples

Signs a string using a non-encrypted RSA private key in base64 encoding. Returns the RSA signature as a base64 string.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    let privKey = CkoPrivateKey()!

    let sbPem = CkoStringBuilder()!
    sbPem.appendLine(str: "-----BEGIN RSA PRIVATE KEY-----", crlf: true)
    sbPem.appendLine(str: "MIIC .... j5A==", crlf: true)
    sbPem.appendLine(str: "-----END RSA PRIVATE KEY-----", crlf: true)

    success = privKey.loadPem(str: sbPem.getAsString())
    if success == false {
        print("\(privKey.lastErrorText!)")
        return
    }

    let rsa = CkoRsa()!

    success = rsa.usePrivateKey(privKey: privKey)
    if success == false {
        print("\(rsa.lastErrorText!)")
        return
    }

    let bd = CkoBinData()!
    bd.appendString(str: "12345678", charset: "utf-8")

    success = rsa.signRawBd(bd: bd)
    if success == false {
        print("\(rsa.lastErrorText!)")
        return
    }

    // Get the base64 RSA signature.
    print("\(bd.getEncoded(encoding: "base64")!)")

    success = rsa.verifyRawBd(bd: bd)
    if success == false {
        print("\(rsa.lastErrorText!)")
        return
    }

    var strOriginal: String? = bd.getString(charset: "utf-8")
    print("\(strOriginal!)")

}