Version: 1.0.2
Target Framework: net10.0+
NuGet Package ID: Marai.Cryptography
Introduction
Marai.Cryptography is a production-ready cryptography library for .NET providing AES-256 encryption across multiple modes, HMAC-SHA256 request signing, and secure PBKDF2 password hashing with a clean, consistent API.
Major capabilities:
- AES-256 encryption and decryption in GCM, CBC, CTR, CFB, and GCMFormatted modes
- SHA-256 hashing of arbitrary text
- Key and IV generation for AES-256
- HMAC-SHA256 request signature creation and validation with replay-attack protection
- Secure PBKDF2/SHA-256 password hashing with randomized salt and iteration count
- Constant-time comparison for signature and password verification
- Microsoft Dependency Injection integration
Table of Contents
- Installation
- Getting Started
- AES Provider
- HMAC Signature Provider
- Password Provider
- Models Reference
- Exceptions Reference
- Security Notes
- Full API Reference
Installation
Getting Started
Register with Dependency Injection
AddMaraiCryptography registers:
ISecurityAESProvider(singleton) — encryption and hashingISecurityHMACSignatureProvider(singleton) — HMAC request signingISecurityPasswordProvider(singleton) — password hashing
Generate a Key and IV
AES Provider
Encryption Modes
| Value | Name | Description | Recommended |
|---|---|---|---|
GCMFormatted |
AES-256-GCM with header | GCM with magic-byte prefix for format detection. Layout: [GCM(3)][nonce(12)][tag(16)][ciphertext] |
Yes — default |
GCM |
AES-256-GCM raw | Authenticated GCM without format header. Layout: [nonce(12)][tag(16)][ciphertext] |
Yes |
CBC |
AES-256-CBC | Block cipher with PKCS7 padding. Layout: [iv(16)][ciphertext] |
Acceptable |
CTR |
AES-256-CTR | Stream cipher mode. Layout: [counter(16)][ciphertext] |
Acceptable |
CFB |
AES-256-CFB | Cipher Feedback mode (CFB-128). Layout: [iv(16)][ciphertext] |
Acceptable |
ECB |
AES-256-ECB | No IV. Identical plaintext blocks produce identical ciphertext. Deprecated. | No |
ECBis marked[Obsolete]in the library. Do not use it for any data where block-level patterns could be revealing.
Default Encrypt and Decrypt
EncryptGcm and DecryptGcm use the key and IV passed to AddMaraiCryptography.
Explicit Mode Encrypt and Decrypt
Keys and IVs are passed as strings. The library derives a fixed-length byte array via SHA-256. Use GenerateAes256KeyAndIv() to produce a proper random key.
SHA-256 Hash
Key and IV Generation
HMAC Signature Provider
Creating a Signature
The payload must be valid JSON. The library wraps it with the current UTC Unix timestamp before signing:
Validating a Signature
ValidateSignature steps:
- Recomputes the expected HMAC using the payload and timestamp.
- Compares using constant-time comparison (
CryptographicOperations.FixedTimeEquals). - Validates the timestamp is within the allowed clock skew and expiration window.
Default timing windows: AllowedClockSkewInMinutes = 5, AllowedExpirationInMinutes = 5.
Password Provider
Hashing a Password
Validating a Password
Models Reference
EncryptionFormats
HmacSecretKey
| Member | Type | Description |
|---|---|---|
Base64 |
string |
Base64-encoded HMAC secret key bytes |
ToBytes() |
byte[] |
Decodes the key to a raw byte array; throws if blank |
HmacSignedRequest
| Member | Type | Description |
|---|---|---|
Payload |
string |
The original JSON request payload |
Signature |
string |
Base64-encoded HMAC-SHA256 signature to validate |
TimeStamp |
UnixTimeSeconds |
Unix timestamp when the signature was created |
SecretKey |
HmacSecretKey |
The shared secret key |
allowedClockSkewInMinutes |
int |
Clock skew tolerance (default: 0, uses config value of 5) |
allowedExpirationInMinutes |
int |
Max request age in minutes (default: 0, uses config value of 5) |
PasswordHash
| Member | Type | Description |
|---|---|---|
HashBase64 |
string |
Base64-encoded PBKDF2 hash output |
SaltBase64 |
string |
Base64-encoded random salt |
Iterations |
int |
PBKDF2 iteration count used when hashing |
UnixTimeSeconds
| Member | Type | Description |
|---|---|---|
Value |
long |
Unix timestamp in seconds |
Now() |
UnixTimeSeconds (static) |
Returns current UTC time as UnixTimeSeconds |
ToDateTimeOffset() |
DateTimeOffset |
Converts to UTC DateTimeOffset |
Exceptions Reference
| Exception | Thrown When |
|---|---|
InvalidSignatureException |
HMAC signature does not match |
ExpiredSignatureException |
Request timestamp is outside the allowed window |
InvalidParameterException |
A required parameter has an invalid value |
ParameterNotFoundException |
A required parameter is missing |
UnknownErrorException |
An unexpected error occurs |
Security Notes
Recommended Modes
- AES-256-GCM / GCMFormatted — Use for all new encryption. GCM provides authenticated encryption, detecting tampering automatically.
- AES-256-CBC / CFB / CTR — Acceptable for interoperability. No built-in authentication; combine with a separate MAC if tamper detection is needed.
- AES-256-ECB — Do not use. Marked
[Obsolete]in the library.
Key and IV Management
- Never hardcode keys in source code. Store them in environment variables, Azure Key Vault, AWS Secrets Manager, or similar.
- Use
GenerateAes256KeyAndIv()to produce cryptographically secure random keys and IVs.
Signature Security
- Uses
CryptographicOperations.FixedTimeEqualsto prevent timing side-channels. - The 5-minute expiration window blocks replay attacks. Tune
allowedExpirationInMinutesas needed. - The secret key should be at least 256 bits (32 bytes) of random data.
Password Hashing
- Uses PBKDF2 with SHA-256, a random 16-byte salt, and a random iteration count between 5,000 and 10,000.
- Password comparison uses constant-time comparison to prevent timing attacks.
Full API Reference
License
This project is licensed under the Marai Proprietary Software License Agreement.
Free for personal and commercial use.
For the complete license agreement and terms, please visit: https://marai.dev/proprietary-software-license-agreement



