Argon2Sharp 1.0.0
See the version list below for details.
dotnet add package Argon2Sharp --version 1.0.0
NuGet\Install-Package Argon2Sharp -Version 1.0.0
<PackageReference Include="Argon2Sharp" Version="1.0.0" />
<PackageVersion Include="Argon2Sharp" Version="1.0.0" />
<PackageReference Include="Argon2Sharp" />
paket add Argon2Sharp --version 1.0.0
#r "nuget: Argon2Sharp, 1.0.0"
#:package Argon2Sharp@1.0.0
#addin nuget:?package=Argon2Sharp&version=1.0.0
#tool nuget:?package=Argon2Sharp&version=1.0.0
<div align="center">
๐ Argon2Sharp
Pure C# Implementation of Argon2 Password Hashing Algorithm
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 9.0 SDK or later
- 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 PHC String Format (Recommended)
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
Recommended for Password Hashing (RFC 9106)
// ... 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
Verifymethods)
โ 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
- RFC 9106 - Argon2 Memory-Hard Function
- RFC 7693 - BLAKE2 Cryptographic Hash
- Argon2 Official GitHub
- OWASP Password Storage Cheat Sheet
๐ Star History
<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 | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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 was computed. 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. |
-
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.