Getting started

edit

Install the package

Add the Meziantou.Framework.Hashing package to a project:

dotnet package add Meziantou.Framework.Hashing

The examples of this documentation omit the using directives: they need using Meziantou.Framework.Hashing;, and using System.Security.Cryptography; for RandomNumberGenerator and CryptographicOperations.

Hash a password

Bcrypt.HashPassword returns a string that contains the algorithm, its cost, a random salt and the hash. Store it, and give it back to Bcrypt.Verify when the user signs in:

var hash = Bcrypt.HashPassword("my password");
var ok = Bcrypt.Verify("my password", hash);
Assert.True(ok);

See BCrypt to choose the cost, and Argon2 for the alternative.

Hash data

The hash functions take a ReadOnlySpan<byte> and return a new array. Sha3.HashData takes the size of the digest in bits:

var digest = Sha3.HashData(256, "abc"u8);
Assert.Equal("3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532", Convert.ToHexStringLower(digest));

Every HashData method also has an overload that writes into a Span<byte> you provide, so you can hash without allocating an array. See Cryptographic hashes.

Compute a checksum

Crc32.Compute returns the CRC-32 as a uint. Crc32.Append continues it with the next part of the data:

var crc = Crc32.Compute("123456789"u8);
Assert.Equal(0xCBF43926u, crc);

// The CRC of data received in several parts
var running = Crc32.Compute("1234"u8);
running = Crc32.Append(running, "56789"u8);
Assert.Equal(crc, running);

See Checksums.

Next steps