Objective-C
Objective-C
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 Objective-C Downloads
#import <CkoJws.h>
#import <CkoJsonObject.h>
#import <NSString.h>
BOOL success = NO;
CkoJws *jws = [[CkoJws alloc] init];
// Protected header specifying HMAC-SHA256.
CkoJsonObject *header = [[CkoJsonObject alloc] init];
[header UpdateString: @"alg" value: @"HS256"];
success = [jws SetProtectedHeader: [NSNumber numberWithInt: 0] json: header];
if (success == NO) {
NSLog(@"%@",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.
CkoJsonObject *unprotected = [[CkoJsonObject alloc] init];
[unprotected UpdateString: @"kid" value: @"signer-key-1"];
success = [jws SetUnprotectedHeader: [NSNumber numberWithInt: 0] json: unprotected];
if (success == NO) {
NSLog(@"%@",jws.LastErrorText);
return;
}
BOOL bIncludeBom = NO;
success = [jws SetPayload: @"This is the content to sign." charset: @"utf-8" includeBom: bIncludeBom];
if (success == NO) {
NSLog(@"%@",jws.LastErrorText);
return;
}
NSString *base64Key = @"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU=";
success = [jws SetMacKey: [NSNumber numberWithInt: 0] key: base64Key encoding: @"base64"];
if (success == NO) {
NSLog(@"%@",jws.LastErrorText);
return;
}
// Because an unprotected header is present, the JWS is produced in a JSON serialization form.
NSString *jwsJson = [jws CreateJws];
if (jws.LastMethodSuccess == NO) {
NSLog(@"%@",jws.LastErrorText);
return;
}
NSLog(@"%@",jwsJson);