Skip to content

Hashing ​

Introduction ​

The Hash facade hashes passwords with PBKDF2-HMAC-SHA256, a random 16-byte salt, and a 32-byte key. Where Laravel defaults to bcrypt, DotBlaze uses PBKDF2 because it ships with .NET and needs no extra package. Authentication uses it to check passwords.

A hash looks like this; the iteration count, salt, and key are stored together, so hashes made with an old iteration count still verify:

$pbkdf2-sha256$600000$<base64 salt>$<base64 key>

Configuration ​

The iteration count comes from the iterations key of config/Hashing.cs. The default is 600,000, the OWASP recommendation for PBKDF2-HMAC-SHA256:

csharp
public class HashingConfig : ConfigFile
{
    public override object Values() => new { iterations = 600000 };
}

NOTE

Hashing is deliberately slow. In tests, lower the count to keep them fast, as the sample app's tests do with builder.UseSetting("hashing:iterations", "1000").

Basic Usage ​

Hashing Passwords ​

csharp
var hashed = Hash.Make("password123");

Verifying That a Password Matches a Hash ​

csharp
if (Hash.Check("password123", user.Password))
{
    // The passwords match...
}

The comparison is constant-time. A value that is not a DotBlaze hash (for example a bcrypt hash imported from Laravel) returns false rather than throwing. A null hash, such as the password of a user without one, also returns false.

Determining if a Password Needs to Be Rehashed ​

NeedsRehash returns true when the hash was made with a different iteration count than the current configuration (or is not a DotBlaze hash). Rehash after a successful login, while you have the plain password:

csharp
if (await Auth.Attempt(new { request.Email, request.Password }))
{
    var user = Auth.User<User>()!;
    if (Hash.NeedsRehash(user.Password))
    {
        user.Password = request.Password;   // the Hashed cast hashes it on save
        await user.Save();
    }
    return Intended("/posts");
}

Checking Whether a Value Is Already Hashed ​

csharp
Hash.IsHashed(user.Password);   // true for a DotBlaze hash

Hashing in Models ​

You rarely call Hash.Make yourself. Mark the password property with the Hashed cast and it is hashed whenever the model is saved; values that are already hashed (Hash.IsHashed) are stored unchanged, so a password is never hashed twice:

csharp
[Fillable, Hidden, Cast(typeof(Hashed))] public string Password { get; set; } = "";
csharp
var user = await User.Create(new { request.Name, request.Email, request.Password });   // stored hashed

See casts.

Complete Example: Verify Password Hashes ​

Create the following Blog command. The hard-coded password is test data, not an account credential.

csharp
// app/Commands/HashDemo.cs
using DotBlaze;

namespace Blog.Commands;

public class HashDemo : Command
{
    public override string Signature => "demo:hash";

    public override Task<int> Handle()
    {
        const string password = "local-example-password";
        var first = Hash.Make(password);
        var second = Hash.Make(password);
        if (first == second || !Hash.Check(password, first) ||
            Hash.Check("wrong-password", first) || !Hash.IsHashed(first))
            throw new InvalidOperationException("Password hash self-check failed.");
        Line("Different salts; correct password accepted; wrong password rejected.");
        return Task.FromResult(0);
    }
}

Run dotblaze demo:hash. It exits with 0; the two hashes differ because their salts differ. In the complete blog, the template's User.Password uses [Hidden, Cast(typeof(Hashed))], so assign the plain password through the model and save it. Do not hash through a bulk query update: that bypasses model casts. Use authentication for the complete sign-in flow, including two-factor challenges.