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

Argon2 Key Derivation with a Secret (Pepper), AD, and Encodings

Demonstrates the optional Argon2 options for Crypt2.Argon2DeriveKey: secret (a pepper), ad (associated data), the encoding member and its per-member overrides saltEncoding/secretEncoding/adEncoding, passwordCharset, and maxMemoryKb.

Background. A pepper is a secret value held by the application and not stored with the hash, so a stolen hash database cannot be attacked without it. The encoding members control how the salt, secret, and ad text are interpreted as bytes (base64 by default; utf-8 uses the characters themselves). maxMemoryKb is a guard against an unreasonable memory cost.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkCrypt2W.h>
#include <C_CkJsonObjectW.h>
#include <C_CkBinDataW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkCrypt2W crypt;
    const wchar_t *password;
    HCkJsonObjectW json;
    HCkBinDataW bdKey;
    const wchar_t *keyHex;

    success = FALSE;

    crypt = CkCrypt2W_Create();

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

    //  Build the Argon2 options JSON, this time using the optional secret, ad, and encoding members.
    //    secret         the RFC 9106 secret value K, often called a "pepper": a key held by the
    //                   application and NOT stored with the hash, so a stolen hash database cannot be
    //                   attacked without it.
    //    ad             optional associated data X: additional non-secret data bound into the derivation.
    //    encoding       how salt, secret, and ad are encoded (default base64).  Per-member overrides are
    //                   saltEncoding, secretEncoding, and adEncoding.
    //    passwordCharset  the charset the password is converted to before use (default utf-8).
    //    maxMemoryKb    a guard (not an Argon2 parameter) refusing to allocate more than this many KB;
    //                   default 2097152 (2 GB).
    json = CkJsonObjectW_Create();

    //  The salt is provided here as hex, overriding the default base64 for just this member.
    CkJsonObjectW_UpdateString(json,L"salt",L"0102030405060708090a0b0c0d0e0f10");
    CkJsonObjectW_UpdateString(json,L"saltEncoding",L"hex");

    //  The pepper is supplied as UTF-8 text (its own characters are the bytes).
    CkJsonObjectW_UpdateString(json,L"secret",L"application-wide-pepper");
    CkJsonObjectW_UpdateString(json,L"secretEncoding",L"utf-8");

    //  Associated data, supplied as UTF-8 text.
    CkJsonObjectW_UpdateString(json,L"ad",L"user-id-42");
    CkJsonObjectW_UpdateString(json,L"adEncoding",L"utf-8");

    CkJsonObjectW_UpdateString(json,L"passwordCharset",L"utf-8");
    CkJsonObjectW_UpdateInt(json,L"maxMemoryKb",1048576);
    CkJsonObjectW_UpdateInt(json,L"keyLen",32);

    bdKey = CkBinDataW_Create();
    success = CkCrypt2W_Argon2DeriveKey(crypt,password,CkJsonObjectW_emit(json),bdKey);
    if (success == FALSE) {
        wprintf(L"%s\n",CkCrypt2W_lastErrorText(crypt));
        CkCrypt2W_Dispose(crypt);
        CkJsonObjectW_Dispose(json);
        CkBinDataW_Dispose(bdKey);
        return;
    }

    keyHex = CkBinDataW_getEncoded(bdKey,L"hex");
    if (CkBinDataW_getLastMethodSuccess(bdKey) == FALSE) {
        wprintf(L"%s\n",CkBinDataW_lastErrorText(bdKey));
        CkCrypt2W_Dispose(crypt);
        CkJsonObjectW_Dispose(json);
        CkBinDataW_Dispose(bdKey);
        return;
    }

    wprintf(L"Derived key: %s\n",keyHex);


    CkCrypt2W_Dispose(crypt);
    CkJsonObjectW_Dispose(json);
    CkBinDataW_Dispose(bdKey);

    }