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 <CkCrypt2W.h>
#include <CkJsonObjectW.h>
#include <CkBinDataW.h>

void ChilkatSample(void)
    {
    bool success = false;

    CkCrypt2W crypt;

    //  The password should come from a secure source rather than being hard-coded.
    const wchar_t *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).
    CkJsonObjectW json;

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

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

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

    json.UpdateString(L"passwordCharset",L"utf-8");
    json.UpdateInt(L"maxMemoryKb",1048576);
    json.UpdateInt(L"keyLen",32);

    CkBinDataW bdKey;
    success = crypt.Argon2DeriveKey(password,json.emit(),bdKey);
    if (success == false) {
        wprintf(L"%s\n",crypt.lastErrorText());
        return;
    }

    const wchar_t *keyHex = bdKey.getEncoded(L"hex");
    if (bdKey.get_LastMethodSuccess() == false) {
        wprintf(L"%s\n",bdKey.lastErrorText());
        return;
    }

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