Sample code for 30+ languages & platforms
Objective-C

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 Objective-C Downloads

Objective-C
#import <CkoJws.h>
#import <CkoJsonObject.h>
#import <CkoBinData.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;
}

BOOL bIncludeBom = NO;
success = [jws SetPayload: @"This is the content to sign." charset: @"utf-8" includeBom: bIncludeBom];
if (success == NO) {
    NSLog(@"%@",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.
CkoBinData *bdKey = [[CkoBinData alloc] init];
[bdKey AppendEncoded: @"YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU=" encoding: @"base64"];
success = [jws SetMacKeyBd: [NSNumber numberWithInt: 0] key: bdKey];
if (success == NO) {
    NSLog(@"%@",jws.LastErrorText);
    return;
}

NSString *jwsCompact = [jws CreateJws];
if (jws.LastMethodSuccess == NO) {
    NSLog(@"%@",jws.LastErrorText);
    return;
}

NSLog(@"%@",jwsCompact);