---
title: "Encode an Argon2 hash as a PHC string in C#"
url: https://recursiveai.org/onecrypto/cookbook/convert-argon2-hash-to-phc-string/
description: "Encode an existing Argon2 hash as a PHC string in C# with the original salt and cost settings, without running Argon2 again."
version: "0.4.0-preview.2"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneCrypto) · [GitHub](https://github.com/recursive-ai-oss/onecrypto-csharp) · [Docs](https://recursiveai.org/onecrypto/docs/) · [Download example (.zip)](https://recursiveai.org/onecrypto/cookbook/convert-argon2-hash-to-phc-string/convert-argon2-hash-to-phc-string.zip)

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

```shell
dotnet add package RecursiveAI.OneCrypto --prerelease
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onecrypto/cookbook/convert-argon2-hash-to-phc-string/convert-argon2-hash-to-phc-string.zip) 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.

```csharp
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.

```csharp
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
```

## Related recipes

- [Hash a password with Argon2](https://recursiveai.org/onecrypto/cookbook/argon2-password-hashing/)
- [Derive a key with scrypt](https://recursiveai.org/onecrypto/cookbook/derive-key-with-scrypt/)

## 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](https://c2sp.org/phc-strings) 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](https://recursiveai.org/onecrypto/docs/kdf/#argon2-phc-strings) 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.
