Sample code for 30+ languages & platforms
Swift

Get a JWS Protected Header

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.GetProtectedH, which copies the decoded protected header of a signature into a JsonObject.

Background. The protected header is integrity-protected by the signature. Inspecting it (for example the alg value) is useful before deciding how to validate.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    let jws = CkoJws()!

    //  Load the JWS compact serialization.
    var jwsCompact: String? = "eyJhbGciOiJIUzI1NiJ9.VGhpcyBpcyB0aGUgY29udGVudCB0byBzaWduLg.<signature>"
    success = jws.load(jwsStr: jwsCompact)
    if success == false {
        print("\(jws.lastErrorText!)")
        return
    }

    //  Copy the decoded protected header of signature 0 into a JsonObject.
    let json = CkoJsonObject()!
    success = jws.getProtectedH(index: 0, json: json)
    if success == false {
        print("\(jws.lastErrorText!)")
        return
    }

    json.emitCompact = false
    print("\(json.emit()!)")

}