Masterly.Hash.Policy 2.0.0

dotnet add package Masterly.Hash.Policy --version 2.0.0
                    
NuGet\Install-Package Masterly.Hash.Policy -Version 2.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Masterly.Hash.Policy" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Masterly.Hash.Policy" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Masterly.Hash.Policy" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Masterly.Hash.Policy --version 2.0.0
                    
#r "nuget: Masterly.Hash.Policy, 2.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Masterly.Hash.Policy@2.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Masterly.Hash.Policy&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Masterly.Hash.Policy&version=2.0.0
                    
Install as a Cake Tool

Masterly.Hash.Policy

Masterly.Hash: password hashing you can't get wrong.

Password quality checks for sign-up and password-change forms, aligned with NIST SP 800-63B rev. 4, plus a zxcvbn-inspired strength estimator. Companion to Masterly.Hash.

dotnet add package Masterly.Hash.Policy

Quickstart

using Masterly.Hash;
using Masterly.Hash.Policy;

var policy = new PasswordPolicy(new PasswordPolicyOptions { ContextWords = { "Contoso" } });

PasswordPolicyResult result = policy.Evaluate(input, new PasswordContext
{
    UserName = user.UserName,
    Email = user.Email,
    DisplayName = user.FullName,
});

if (!result.IsAcceptable)
    return BadRequest(new { errors = result.Violations.Select(v => v.Message), result.Suggestions });

user.PasswordHash = Password.Hash(input);

With a breached-password corpus (you supply the implementation, e.g. a Pwned Passwords k-anonymity client):

var policy = new PasswordPolicy(new PasswordPolicyOptions(), myBreachChecker);
var result = await policy.EvaluateAsync(input, context, cancellationToken);
if (result.BreachCheckUnavailable)
    logger.LogWarning("Breach corpus unavailable; password accepted on local checks only.");

Stand-alone strength meter:

StrengthEstimate estimate = PasswordStrengthEstimator.Estimate(input, new[] { user.UserName });
// estimate.Score (0-4), estimate.GuessesLog10, estimate.CrackTimes.OfflineSlowHash.Display,
// estimate.Warning, estimate.Suggestions

Rules

Check Default Violation
Minimum length, in Unicode code points after NFKC 15 (NIST, password as the only factor). 8 is allowed when the password is one factor of MFA; lower values are refused. TooShort
Maximum length 128 (must be 64-4096; NIST: permit at least 64) TooLong
Blocklist: the 10,000 most common passwords, case- and l33t-insensitive, plus BlockedPasswords on CommonPassword
Context words: user name, email local part and domain, display name, service name, extra words; also reversed, and l33t-insensitive on both sides (j0hnd03 blocks johndoe and vice versa) on, words of 4+ code points ContainsContextWord
Only repeats of short or patterned units, sequences and keyboard runs (aaaaaa, aabaabaab, 1234abcd, qwertyuiop), checked over the whole password. A strong token typed twice is left to the score. on RepetitiveOrSequential
Strength score below MinimumScore 3 (≥ 10^8 estimated guesses) TooWeak
Breached-password lookup (EvaluateAsync with an IBreachedPasswordChecker) off until you supply one Breached

There are no composition rules (upper case, digit, symbol) and no expiry: NIST SP 800-63B says verifiers SHALL NOT impose them. Force a change only on evidence of compromise. All characters, including spaces and emoji, are allowed.

Strength estimator

PasswordStrengthEstimator is inspired by zxcvbn (Wheeler, USENIX Security 2016). It is an independent implementation of the same approach, not a port, and its numbers differ from zxcvbn's:

  • matchers for dictionary words (embedded common-password list ranked by frequency, a small English word list and your context words), reversed words, l33t substitutions, QWERTY and keypad patterns, sequences, repeats, years and dates, with bruteforce for the rest;
  • a dynamic-programming search for the decomposition needing the fewest guesses;
  • GuessesLog10, a 0-4 Score with zxcvbn's thresholds (10^3, 10^6, 10^8, 10^10), crack-time estimates for online throttled (100/hour), online unthrottled (10/s), offline slow hash (10^4/s) and offline fast hash (10^10/s) attacks, and warning/suggestion text.

Only the first 100 characters are analyzed (IsTruncated tells you), which bounds the cost of a call; the estimate of a longer password is that of its first 100 characters, an approximation rather than a bound. The policy's own rules (length, blocklist, context words, repetitive/sequential) always look at the whole password.

Security notes

  • PasswordPolicyResult.ToString(), IsAcceptable, Score, Violations, Suggestions and StrengthEstimate.Warning never contain any part of the password and are safe to log.
  • StrengthEstimate details are derived from the password: Sequence gives the position and length of each word, and the guess count of a weak password reflects the rank of the list entry it matched. Show them to the user who typed the password, but do not log, serialize or store result.StrengthEstimate. To limit leakage, guesses and crack times are rounded down to a tenth of a decade and individual patterns carry no guess counts.
  • IBreachedPasswordChecker implementations must not send the password anywhere; use k-anonymity (send a short hash prefix only). A checker that throws or returns Unavailable does not block the user; the outcome is reported in BreachCheck.
  • Strength estimates model a guessing attacker; they are heuristics, not guarantees. The real defence against offline attacks is the slow hash, so keep hashing with Masterly.Hash.
  • SensitiveString overloads keep passwords out of logs in request models.

Third-party material: see THIRD-PARTY-NOTICES.txt (SecLists, MIT).

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Masterly.Hash.Policy:

Package Downloads
Masterly.Hash.Pwned

Have I Been Pwned "Pwned Passwords" k-anonymity client for Masterly.Hash: implements IBreachedPasswordChecker from Masterly.Hash.Policy. Sends only the first 5 hex characters of the password's SHA-1 hash, requests padded responses, fails open (or throws) on outages and timeouts, and offers a bounded in-memory range cache plus an AddPwnedPasswords() typed HttpClient registration.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0 113 11/3/2026