Hashing in ASP.NET Core 2.0

Problem

How to hash data (e.g. passwords) in ASP.NET Core.

Solution

The new Data Protection API in .NET Core includes functionality to create hashes using PBKDF2 algorithm. ASP.NET Core uses this behind the scenes in PasswordHasher class, which is used in ASP.NET Core Identity.

Create a class library and add NuGet package: Microsoft.AspNetCore.Cryptography.KeyDerivation

Add a class to encapsulate hashing logic:

Add a class to encapsulate logic for creating a random salt:

Add tests to verify the functionality e.g.:

Source Code

GitHub: https://github.com/TahirNaushad/Fiver.Security.Hashing

Leave a Reply