Recursive AI

Encode an Argon2 hash as a PHC string in C#

OneCrypto is a free, open-source cryptography library for .NET from Recursive AI. MIT or Apache-2.0, preview, no native dependencies.

A PHC string is text like $argon2id$v=19$m=…,t=…,p=…$salt$hash that holds the hash, its salt, and its cost settings.

Terminal
dotnet add package RecursiveAI.OneCrypto --prerelease

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneCrypto.Primitives;
byte[] hash = Convert.FromHexString(
    "B85C889A0607C23D8A7262E76EC13FD4");
Console.WriteLine(Argon2Phc.Format(
    new(Argon2Type.Argon2id, 65536, 3, 1,
    "example-salt-123"u8.ToArray(), hash)));

The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.

Text
$argon2id$v=19$m=65536,t=3,p=1$ZXhhbXBsZS1zYWx0LTEyMw$uFyImgYHwj2KcmLnbsE/1A

The full program

The download derives the same demo hash, formats it and verifies it. The fixed password and salt are only for reproducing the example.

C#
using RecursiveAI.OneCrypto.Primitives;
 
// Fixed inputs for a reproducible example only.
var type = Argon2Type.Argon2id;
byte[] salt = "example-salt-123"u8.ToArray();
byte[] hash = Argon2.DeriveKey(type,
    "example-password"u8, salt, 65536, 3, 1, 16);
var value = new Argon2PhcHash(type, 65536, 3, 1, salt, hash);
string phc = Argon2Phc.Format(value);
Console.WriteLine(phc);
Console.WriteLine($"Raw tag: {Convert.ToHexString(hash)}");
Console.WriteLine($"Matches: {Argon2Phc.Verify("example-password"u8, phc)}");

dotnet run prints:

Text
$argon2id$v=19$m=65536,t=3,p=1$ZXhhbXBsZS1zYWx0LTEyMw$uFyImgYHwj2KcmLnbsE/1A
Raw tag: B85C889A0607C23D8A7262E76EC13FD4
Matches: True

Format throws ArgumentException when supplied fields are outside the allowed ranges, for example a salt shorter than eight bytes.

Variations

Read the cost settings back. Parsing does not require the password or run the derivation.

C#
using RecursiveAI.OneCrypto.Primitives;
 
string phc = "$argon2id$v=19$m=65536,t=3,p=1$"
    + "ZXhhbXBsZS1zYWx0LTEyMw$uFyImgYHwj2KcmLnbsE/1A";
Console.WriteLine(Argon2Phc.Parse(phc).MemoryKiB);
Text
65536

The short answer

Create an Argon2PhcHash with the original Argon2 variant, memory in KiB, passes, lanes, salt bytes and raw hash bytes, then call Argon2Phc.Format. This API writes Argon2 version 19. If you have an older version or don’t know the salt or parameters, don’t fill in defaults: they describe the original computation and are needed to reproduce the hash.

What is an Argon2 PHC string?

The PHC string specification defines a text format for password hashes. In the example, $argon2id names the variant, v=19 is the decimal form of version 0x13, and m=65536,t=3,p=1 records memory in KiB, the number of passes and the number of lanes. The last two fields are the salt and the tag (the derived hash), in standard Base64 without = padding. They are not hex or Base64URL.

If you are converting an existing record, decode its stored hash and salt to bytes first. The snippet uses Convert.FromHexString because its input tag is hex; passing the characters of the hex text as bytes would encode a different tag. Use the same variant and costs the old record used. Formatting leaves the underlying hash unchanged, so it cannot raise an old record’s work factor.

How can I create an Argon2 hash instead of encoding one I already have?

Use Argon2Phc.Hash when you have a password and need a new stored hash. It generates a fresh random salt and returns the finished string. Format is for existing bytes; it does not generate a salt or need the password. If you need to upgrade an older record’s costs, derive a new hash from the password rather than editing m, t or p in the string.

How do I verify an Argon2id PHC string in C#?

Argon2Phc.Verify(password, phc) uses the algorithm, costs and salt in the stored string and compares the recomputed bytes with the stored tag. It isn’t a way to decrypt or recover the password. Keep the string away from logs, even though it does not contain the plaintext password.

OneCrypto’s PHC parser requires v=19, the m,t,p order and canonical unpadded Base64. A malformed value raises Argon2PhcFormatException (TryParse returns false). A string can parse yet request more memory or work than your service can allow: configure KdfLimits for verification. The limits apply before derivation; parsing alone does not verify a password.

All onecrypto recipes