Sample code for 30+ languages & platforms
Swift

Sign a JWS with an HMAC Key from BinData

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.SetMacKeyBd, which sets the symmetric MAC key for a signature from the raw bytes in a BinData.

Background. This is the in-memory equivalent of SetMacKey, for when the key material is already available as bytes.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    let jws = CkoJws()!

    //  Protected header specifying HMAC-SHA256.
    let header = CkoJsonObject()!
    header.updateString(jsonPath: "alg", value: "HS256")
    success = jws.setProtectedHeader(index: 0, json: header)
    if success == false {
        print("\(jws.lastErrorText!)")
        return
    }

    var bIncludeBom: Bool = false
    success = jws.setPayload(payload: "This is the content to sign.", charset: "utf-8", includeBom: bIncludeBom)
    if success == false {
        print("\(jws.lastErrorText!)")
        return
    }

    //  Set the symmetric MAC key for signature 0 from the raw bytes in a BinData.  In production, obtain
    //  the key material from a secure source.
    let bdKey = CkoBinData()!
    bdKey.appendEncoded(encData: "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU=", encoding: "base64")
    success = jws.setMacKeyBd(index: 0, key: bdKey)
    if success == false {
        print("\(jws.lastErrorText!)")
        return
    }

    var jwsCompact: String? = jws.create()
    if jws.lastMethodSuccess == false {
        print("\(jws.lastErrorText!)")
        return
    }

    print("\(jwsCompact!)")

}