Swift
Swift
Set the Optional JWS Unprotected Header
See more JSON Web Signatures (JWS) Examples
Demonstrates Jws.SetUnprotectedHeader, which sets the optional unprotected header for a signature.
Background. Unlike the protected header, the unprotected header is not covered by the signature. It is carried in the JSON serialization forms of a JWS, so producing a JWS with an unprotected header yields a JSON serialization rather than compact form.
Chilkat Swift Downloads
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
}
// Set the optional unprotected header for signature 0. Unlike the protected header, it is not
// covered by the signature. It is carried in the JSON serialization forms of a JWS.
let unprotected = CkoJsonObject()!
unprotected.updateString(jsonPath: "kid", value: "signer-key-1")
success = jws.setUnprotectedHeader(index: 0, json: unprotected)
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
}
var base64Key: String? = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU="
success = jws.setMacKey(index: 0, key: base64Key, encoding: "base64")
if success == false {
print("\(jws.lastErrorText!)")
return
}
// Because an unprotected header is present, the JWS is produced in a JSON serialization form.
var jwsJson: String? = jws.create()
if jws.lastMethodSuccess == false {
print("\(jws.lastErrorText!)")
return
}
print("\(jwsJson!)")
}