Argon2Sharp 2.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Argon2Sharp --version 2.0.0
                    
NuGet\Install-Package Argon2Sharp -Version 2.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Argon2Sharp" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Argon2Sharp" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Argon2Sharp" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Argon2Sharp --version 2.0.0
                    
#r "nuget: Argon2Sharp, 2.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Argon2Sharp@2.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Argon2Sharp&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Argon2Sharp&version=2.0.0
                    
Install as a Cake Tool

<div align="center">

๐Ÿ” Argon2Sharp

Pure C# Implementation of Argon2 Password Hashing Algorithm

.NET 8.0 .NET 9.0 .NET 10.0 Build Status Tests Code Coverage License RFC 9106

A modern, high-performance, pure C# implementation of the Argon2 password hashing algorithm following RFC 9106 specification. Built with .NET 9 and designed for security-critical applications.

Features โ€ข Installation โ€ข Quick Start โ€ข Documentation โ€ข Benchmarks โ€ข Contributing

</div>


โœจ Features

<table> <tr> <td>

๐ŸŽฏ Algorithm Support

  • Argon2d (data-dependent)
  • Argon2i (data-independent)
  • Argon2id (hybrid - recommended)

</td> <td>

โšก Performance

  • Zero-allocation paths
  • Span<T> optimizations
  • Parallel processing support

</td> </tr> <tr> <td>

๐Ÿ”’ Security

  • RFC 9106 compliant
  • Constant-time comparisons
  • Automatic memory cleanup

</td> <td>

๐Ÿ› ๏ธ Developer Experience

  • Simple & advanced APIs
  • PHC string format support
  • Comprehensive documentation

</td> </tr> </table>

Key Highlights

Feature Description
๐Ÿš€ Pure C# No native dependencies, runs anywhere .NET runs
๐Ÿ“ฆ Zero Dependencies Self-contained implementation
๐ŸŽจ Modern Syntax Built with C# 12 and .NET 9
๐Ÿงช Well Tested 34 unit tests with 95%+ coverage
๐Ÿ“š Documentation Comprehensive XML docs and examples
๐Ÿ”ง Flexible Configurable memory, iterations, and parallelism

๐Ÿ“ฆ Installation

Via Git Clone

git clone https://github.com/Paol0B/Argon2id.git
cd Argon2id
dotnet build Argon2Sharp/Argon2Sharp.csproj

Build from Source

# Build in Release mode
dotnet build -c Release

# Run tests
dotnet test

# Run examples
dotnet run --project Argon2Sharp.Examples

Requirements

  • .NET 8.0, 9.0, or 10.0 SDK
  • C# 12 compatible compiler

๐Ÿš€ Quick Start

Basic Password Hashing

using Argon2Sharp;

// Hash a password with default parameters (Argon2id, 19MB, 2 iterations)
byte[] hash = Argon2.HashPassword("MyPassword123", out byte[] salt);

// Verify password
var parameters = Argon2Parameters.CreateDefault();
parameters.Salt = salt;
var argon2 = new Argon2(parameters);
bool isValid = argon2.Verify("MyPassword123", hash);  // โœ… true
using Argon2Sharp;

// Hash password to PHC format string
string phcHash = Argon2PhcFormat.HashPassword("MyPassword123");
// Output: $argon2id$v=19$m=19456,t=2,p=1$...salt...$...hash...

// Verify password (constant-time comparison)
bool isValid = Argon2PhcFormat.VerifyPassword("MyPassword123", phcHash);  // โœ… true

๐Ÿ“– Documentation

Advanced Usage

<details> <summary><b>Custom Parameters</b></summary>

using Argon2Sharp;

var parameters = new Argon2Parameters
{
    Type = Argon2Type.Argon2id,        // Algorithm variant
    MemorySizeKB = 65536,               // 64 MB
    Iterations = 4,                     // Time cost
    Parallelism = 4,                    // Threads
    HashLength = 32,                    // Output size
    Salt = Argon2.GenerateSalt(16)     // 16-byte salt
};

var argon2 = new Argon2(parameters);
byte[] hash = argon2.Hash("MyPassword123");

</details>

<details> <summary><b>High Security Configuration</b></summary>

using Argon2Sharp;

// Use high security preset (64MB, 4 iterations, 4 threads)
var parameters = Argon2Parameters.CreateHighSecurity();
parameters.Salt = Argon2.GenerateSalt(16);

var argon2 = new Argon2(parameters);
byte[] hash = argon2.Hash("MyPassword123");

</details>

<details> <summary><b>With Secret Key and Associated Data</b></summary>

using Argon2Sharp;
using System.Text;

var parameters = new Argon2Parameters
{
    Type = Argon2Type.Argon2id,
    MemorySizeKB = 32768,
    Iterations = 3,
    Parallelism = 4,
    HashLength = 32,
    Salt = Argon2.GenerateSalt(16),
    Secret = Encoding.UTF8.GetBytes("app-secret-key"),
    AssociatedData = Encoding.UTF8.GetBytes("user-context")
};

var argon2 = new Argon2(parameters);
byte[] hash = argon2.Hash("MyPassword123");

</details>

Algorithm Variants

Variant Use Case Security Profile
Argon2id ๐ŸŒŸ General password hashing Hybrid - resistant to both GPU and side-channel attacks
Argon2i Side-channel sensitive Data-independent - maximum side-channel resistance
Argon2d Cryptocurrency/KDF Data-dependent - maximum GPU attack resistance

๐Ÿ’ก Recommendation: Use Argon2id for password hashing (RFC 9106 recommendation)

Parameter Guidelines

// ... other parameters

};


**Best for:** Cryptocurrency mining, KDF where side-channels aren't a concern

## Parameter Guidelines

### Recommended for Password Hashing (RFC 9106)

```csharp
var parameters = new Argon2Parameters
{
    Type = Argon2Type.Argon2id,
    MemorySizeKB = 19456,      // 19 MB (minimum recommended)
    Iterations = 2,             // 2 passes (minimum recommended)
### Parameter Guidelines

#### Recommended for Password Hashing (RFC 9106)

```csharp
var parameters = new Argon2Parameters
{
    Type = Argon2Type.Argon2id,
    MemorySizeKB = 19456,      // 19 MB (minimum recommended)
    Iterations = 2,             // 2 passes (minimum recommended)
    Parallelism = 1,            // Single-threaded
    HashLength = 32             // 256-bit output
};
Parameter Constraints
Parameter Minimum Recommended Maximum
Memory Size 8 KB โ‰ฅ 19 MB System dependent
Iterations 1 โ‰ฅ 2 Unlimited
Parallelism 1 1-4 16,777,215
Hash Length 4 bytes 32-64 bytes Unlimited
Salt Length 8 bytes โ‰ฅ 16 bytes Unlimited

โšก Performance

Benchmarks on Intel Core i7 (typical modern CPU):

Memory Iterations Parallelism Time Security Level
32 KB 3 4 ~5-10 ms โš ๏ธ Testing only
1 MB 3 4 ~50-100 ms โš ๏ธ Low security
19 MB 2 1 ~100-200 ms โœ… Recommended
64 MB 4 4 ~500-1000 ms ๐Ÿ”’ High security

๐Ÿ’ก Tip: Adjust parameters based on your threat model and available resources. Higher values = better security but slower performance.

๐Ÿงช Testing

# Run all tests
dotnet test

# Run with detailed output
dotnet test --verbosity detailed

# Run specific test class
dotnet test --filter "FullyQualifiedName~Argon2Rfc9106Tests"

Test Coverage

  • โœ… 34 unit tests covering all algorithm variants
  • โœ… RFC 9106 test vectors validation
  • โœ… Edge cases and parameter validation
  • โœ… PHC format encoding/decoding
  • โœ… 95%+ code coverage

๐Ÿ—๏ธ Architecture

Argon2 Class

Main class for hashing operations.

public sealed class Argon2
{
    public Argon2(Argon2Parameters parameters);
    
    public byte[] Hash(string password);
    public byte[] Hash(byte[] password);
    public void Hash(ReadOnlySpan<byte> password, Span<byte> output);
    
    public bool Verify(string password, byte[] hash);
    public bool Verify(byte[] password, byte[] hash);
    
    public static byte[] HashPassword(string password, out byte[] salt);
    public static bool VerifyPassword(string password, byte[] hash, byte[] salt, ...);
    public static byte[] GenerateSalt(int length = 16);
## ๐Ÿ—๏ธ Architecture

Argon2Sharp/ โ”œโ”€โ”€ Core/ โ”‚ โ”œโ”€โ”€ Blake2b.cs # Blake2b-512 hash implementation โ”‚ โ”œโ”€โ”€ Argon2Core.cs # Core compression & permutation functions โ”‚ โ””โ”€โ”€ Argon2Engine.cs # Main algorithm orchestration โ”œโ”€โ”€ Argon2.cs # Public API interface โ”œโ”€โ”€ Argon2Parameters.cs # Configuration & presets โ”œโ”€โ”€ Argon2Types.cs # Type & version enumerations โ””โ”€โ”€ Argon2PhcFormat.cs # PHC string encoding/decoding


### Key Components

- **Blake2b**: Pure C# implementation of Blake2b-512 for internal hashing
- **Argon2Core**: Low-level block operations with G function and P permutation
- **Argon2Engine**: Memory initialization, block filling, and finalization
- **Memory Management**: Efficient pooling with `ArrayPool<T>` and automatic cleanup

## ๐Ÿ“š API Reference

<details>
<summary><b>Argon2 Class</b></summary>

Main class for hashing operations.

```csharp
public sealed class Argon2
{
    public Argon2(Argon2Parameters parameters);
    
    // Hash methods
    public byte[] Hash(string password);
    public byte[] Hash(byte[] password);
    public void Hash(ReadOnlySpan<byte> password, Span<byte> output);
    
    // Verify methods (constant-time comparison)
    public bool Verify(string password, byte[] hash);
    public bool Verify(byte[] password, byte[] hash);
    
    // Static convenience methods
    public static byte[] HashPassword(string password, out byte[] salt);
    public static bool VerifyPassword(string password, byte[] hash, byte[] salt, ...);
    public static byte[] GenerateSalt(int length = 16);
    public static string ToBase64(byte[] hash);
    public static byte[] FromBase64(string base64Hash);
}

</details>

<details> <summary><b>Argon2Parameters Class</b></summary>

Configuration parameters for Argon2.

public sealed class Argon2Parameters
{
    public Argon2Type Type { get; set; }
    public Argon2Version Version { get; set; }
    public int MemorySizeKB { get; set; }
    public int Iterations { get; set; }
    public int Parallelism { get; set; }
    public int HashLength { get; set; }
    public byte[]? Salt { get; set; }
    public byte[]? Secret { get; set; }
    public byte[]? AssociatedData { get; set; }
    
    // Factory methods
    public static Argon2Parameters CreateDefault();        // 19MB, 2 iterations
    public static Argon2Parameters CreateHighSecurity();   // 64MB, 4 iterations
    public static Argon2Parameters CreateForTesting();     // 32KB, 3 iterations
    
    public void Validate();
    public Argon2Parameters Clone();
}

</details>

<details> <summary><b>Argon2PhcFormat Class</b></summary>

PHC string format encoding/decoding.

public static class Argon2PhcFormat
{
    public static string Encode(byte[] hash, byte[] salt, Argon2Type type, ...);
    public static bool TryDecode(string phcString, out byte[]? hash, out byte[]? salt, ...);
    public static string HashPassword(string password, int memorySizeKB = 19456, ...);
    public static bool VerifyPassword(string password, string phcHash);
}

PHC Format:

$argon2id$v=19$m=19456,t=2,p=1$base64salt$base64hash

</details>

๐Ÿ”’ Security Considerations

Best Practices

โœ… DO:

  • Use Argon2id for password hashing (recommended by RFC 9106)
  • Generate cryptographically random salts using Argon2.GenerateSalt()
  • Store salt alongside the hash (they're not secret)
  • Use PHC string format for easy storage and portability
  • Tune parameters based on your threat model and available resources
  • Use constant-time comparison (built into Verify methods)

โŒ DON'T:

  • Reuse salts across different passwords
  • Use predictable salts (timestamps, user IDs, etc.)
  • Store passwords in plain text (obviously!)
  • Use insufficient memory or iterations for production
  • Ignore parameter validation errors

Parameter Tuning Guide

// Low security (testing only) - NOT for production
var testParams = Argon2Parameters.CreateForTesting();  // 32KB, 3 iterations

// Moderate security (minimum recommended)
var defaultParams = Argon2Parameters.CreateDefault();  // 19MB, 2 iterations

// High security (sensitive applications)
var highSecParams = Argon2Parameters.CreateHighSecurity();  // 64MB, 4 iterations

// Custom tuning
var customParams = new Argon2Parameters
{
    Type = Argon2Type.Argon2id,
    MemorySizeKB = 131072,  // 128 MB
    Iterations = 5,          // 5 passes
    Parallelism = 8,         // 8 threads
    HashLength = 64          // 512-bit output
};

Threat Model Considerations

Threat Mitigation Configuration
Online attacks Rate limiting + basic Argon2 Default parameters (19MB, 2 iter)
Offline attacks High memory cost 64-128MB, 3-5 iterations
GPU attacks Argon2id/d with high memory Use Argon2id, โ‰ฅ64MB
Side-channel attacks Argon2i or Argon2id Use Argon2id for best balance
Compromised database Strong parameters + unique salts Always use random salts

๐Ÿค Contributing

Contributions are welcome! Please ensure:

  • โœ… Code follows C# coding conventions and .NET 9 best practices
  • โœ… All tests pass (dotnet test)
  • โœ… New features include comprehensive tests
  • โœ… XML documentation is updated
  • โœ… README is updated for significant changes

Development Setup

# Clone repository
git clone https://github.com/Paol0B/Argon2id.git
cd Argon2id

# Build
dotnet build

# Run tests
dotnet test

# Run examples
dotnet run --project Argon2Sharp.Examples

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

  • Based on the Argon2 specification by Alex Biryukov, Daniel Dinu, and Dmitry Khovratovich
  • Follows RFC 9106 - Argon2 Memory-Hard Function for Password Hashing
  • Implements RFC 7693 - BLAKE2 Cryptographic Hash

๐Ÿ“– References

๐ŸŒŸ Star History

Star History Chart


<div align="center">

Made with โค๏ธ by Paolo

If you find this project useful, please consider giving it a โญ!

Report Bug โ€ข Request Feature โ€ข Documentation

</div>

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Argon2Sharp:

Package Downloads
SPTarkov.Server.Web

Common shared library for the Single Player Tarkov projects.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.0.1 5,750 1/8/2026
4.0.0 173 1/1/2026
3.5.0 736 12/1/2025
3.0.0 272 11/25/2025
2.0.0 345 11/12/2025
1.0.0 226 11/2/2025