Crypto
:books: Provides a handy way to hash data using Adler32, CRC (8-bit to 82-bit, and customizable without bit restriction), MD5, SHA-1, SHA-2 and SHA-3 (256-bit, 384-bit and 512-bit), including HMAC keyed hashing for some types. Functions for encrypting and decrypting data with Rijndael (128-bit, 192-bit and 256-bit) are also offered
Install / Use
/learn @Roydl/CryptoREADME
Roydl.Crypto
Roydl.Crypto provides a simple, generic way to hash virtually any .NET type. Generic extensions are available for almost every type, and a growing set of algorithms is offered — some of which are performance-optimized and likely more powerful than any other pure C# library of their kind.
Table of Contents
- Prerequisites
- Install
- Checksum Algorithms
- Checksum Performance
- Usage
- Encryption Algorithms
- Would you like to help?
Prerequisites
- .NET 10 LTS or higher
- Supported platforms: Windows, Linux, macOS
- Hardware acceleration (optional): SSE4.2, SSSE3, AVX2, or AVX-512 capable CPU
Install
$ dotnet add package Roydl.Crypto
Checksum Algorithms
| Name | Bit Width | Algorithm | Type | Hardware Support | | :---- | ----: | :---- | :---- | :----: | | Adler-32 | 32-bit | Standard | Cyclic | AVX-512 <br> AVX2 <br> SSSE3 | | CRC | from 8-bit<br>to 82-bit | 88 presets available + customizable | Cyclic | iSCSI @ SSE4.2 CPU <br> iSCSI+PKZip @ ARM | | MD5 | 128-bit | Built-in + HMAC keyed-hash support | Cryptographic | See ¹ | | SHA1 | 160-bit | Built-in + HMAC keyed-hash support | Cryptographic | See ¹ | | SHA2 | 256-bit<br>384-bit<br>512-bit | Built-in + HMAC keyed-hash support | Cryptographic | See ¹ | | SHA3 | 256-bit<br>384-bit<br>512-bit | Built-in + HMAC keyed-hash support | Cryptographic | See ¹ |
¹ Hardware acceleration for cryptographic algorithms is handled transparently by the .NET runtime and depends on the underlying platform (e.g. Intel SHA Extensions for SHA1 and SHA2-256 on supported CPUs).
Checksum Performance
Especially for Adler and CRC, the performance in software mode should be much better than with any other pure C# library, but similar to libraries that work with C/C++ imports. However, I couldn't find any other library with hardware support, not even with imports.
| Algorithm | Library | Mode | Speed | | :---- | :----: | :----: | ----: | | Adler-32 | This | Software | 3028.4 MiB/s | | Adler-32 | This | Hardware (SSSE3) | 36449.9 MiB/s | | Adler-32 | This | Hardware (AVX2) | 73427.1 MiB/s | | CRC-32 | Crc32.NET | Software | 2334.3 MiB/s | | CRC-32 | This | Software | 3098.6 MiB/s | | CRC-32 | This | Hardware | 12910.5 MiB/s | | SHA2-256 | Built-in | Software | 2282.7 MiB/s | | SHA3-256 | Built-in | Software | 670.5 MiB/s |
<details> <summary>Benchmark methodology</summary>| Component | Details |
| :--- | :--- |
| CPU | AMD Ryzen 5 7600 (6C/12T, 5.1 GHz boost) |
| RAM | 32 GB DDR5 |
| OS | Manjaro Linux (Kernel 6.19.2-1) |
| Runtime | .NET 10 |
| Build | Release (dotnet run -c Release) |
In the test case, a 64 KiB packet with random bytes is generated, which is sent over and over again within 9 seconds by the function that computes the hash. During this process, it is determined several times how much data could be hashed within 1 second. 9 seconds proved to be the optimal duration — increasing this time does not provide more accurate results. Running multiple repetitions yields more reliable results: all individual values are recorded, outliers identified via min/max comparison, and the final result taken as the average of 20 runs. You can find the test case here.
</details>Usage
GetChecksum & GetCipher
The GetChecksum extension method retrieves a string representation of the computed hash.
_The value can be almost anything: bool, sbyte, byte, short, ushort, char, int, uint, long, ulong, Half, float
