Sample code for 30+ languages & platforms
Objective-C Requires Chilkat v11.6.0+

Hash a Password with Argon2 (Tuned Options)

Demonstrates Crypt2.Argon2HashPassword with explicit options: the cost parameters, saltLen (how many random salt bytes to generate when no salt is given), and an optional secret (pepper).

Background. For hashing, the salt is optional — when omitted, a random salt of saltLen bytes (default 16) is generated, which is the normal case. A secret or ad is not stored in the PHC string; the same values must be supplied to Argon2VerifyPassword for the password to verify.

Chilkat Objective-C Downloads

Objective-C
#import <CkoCrypt2.h>
#import <NSString.h>
#import <CkoJsonObject.h>

CkoCrypt2 *crypt = [[CkoCrypt2 alloc] init];

//  The password should come from a secure source rather than being hard-coded.
NSString *password = @"correct horse battery staple";

//  The Argon2 options are the same members as Argon2DeriveKey, except the salt is optional for
//  hashing.  When no "salt" is given, a random salt is generated; "saltLen" (default 16, range 8..1024)
//  sets how many random bytes to use.
CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json UpdateString: @"variant" value: @"argon2id"];
[json UpdateInt: @"memoryCostKb" value: [NSNumber numberWithInt: 131072]];
[json UpdateInt: @"iterations" value: [NSNumber numberWithInt: 4]];
[json UpdateInt: @"parallelism" value: [NSNumber numberWithInt: 2]];
[json UpdateInt: @"saltLen" value: [NSNumber numberWithInt: 16]];

//  A secret (pepper) and/or ad may be used, but neither is stored in the returned PHC string.  The
//  same values must be supplied to Argon2VerifyPassword for the password to verify.
[json UpdateString: @"secret" value: @"application-wide-pepper"];
[json UpdateString: @"secretEncoding" value: @"utf-8"];

NSString *phcHash = [crypt Argon2HashPassword: password json: [json Emit]];
if (crypt.LastMethodSuccess == NO) {
    NSLog(@"%@",crypt.LastErrorText);
    return;
}

NSLog(@"%@",phcHash);