BCryptExtensions 1.0.1
dotnet add package BCryptExtensions --version 1.0.1
NuGet\Install-Package BCryptExtensions -Version 1.0.1
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="BCryptExtensions" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BCryptExtensions" Version="1.0.1" />
<PackageReference Include="BCryptExtensions" />
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 BCryptExtensions --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: BCryptExtensions, 1.0.1"
#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 BCryptExtensions@1.0.1
#: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=BCryptExtensions&version=1.0.1
#tool nuget:?package=BCryptExtensions&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
BCryptExtensions
基于 BCrypt.Net-Next 的字符串扩展库,提供简洁的密码哈希与验证 API,并内置灵活的密码强度验证规则。
支持 .NET 8 和 .NET 10。
安装
dotnet add package BCryptExtensions
快速开始
using BCryptExtensions;
// 哈希
var hash = "mypassword".ToBCrypt();
var hash = "mypassword".ToBCrypt(workFactor: 12);
// 验证
bool valid = "mypassword".VerifyBCrypt(hash);
// 增强型哈希与验证
var enhancedHash = "mypassword".ToBCryptEnhanced();
bool validEnhanced = "mypassword".VerifyBCryptEnhanced(enhancedHash);
// 验证 + 自动检测是否需要升级工作因子
if ("mypassword".VerifyBCrypt(storedHash, expectedWorkFactor: 12, out bool needsRehash))
{
if (needsRehash)
{
var newHash = "mypassword".ToBCrypt(12);
// 持久化 newHash
}
}
密码验证规则
内置密码强度验证,默认规则:
- 长度 8–20 个字符
- 必须包含 字母、数字、特殊符号 中的 至少两种
基本验证
bool isValid = "P@ssw0rd".ValidatePassword(); // true
bool isValid = "12345678".ValidatePassword(); // false(只有数字)
带错误信息的验证
if (!password.ValidatePassword(out string? error))
{
Console.WriteLine($"密码不符合要求: {error}");
}
// 输出:密码必须包含字母、数字、特殊符号中的至少两种类型。
一步到位:验证并生成哈希
string? hash = "P@ssw0rd".ToBCryptIfValid();
if (hash != null)
{
// 保存哈希
}
// 使用自定义工作因子
string? hash = "P@ssw0rd".ToBCryptIfValid(workFactor: 12);
// 增强型
string? hash = "P@ssw0rd".ToBCryptEnhancedIfValid();
自定义验证规则
通过 PasswordValidationOptions 灵活配置验证规则:
预设规则模板
// 默认规则(8-20,至少2种类型)
PasswordValidationOptions.Default
// 严格模式(10-20,至少3种类型)
PasswordValidationOptions.Strict
// 宽松模式(6-30,至少1种类型)
PasswordValidationOptions.Loose
// 不允许符号(仅字母+数字)
PasswordValidationOptions.NoSymbols
使用自定义规则
var options = new PasswordValidationOptions
{
MinLength = 12,
MaxLength = 24,
MinCharTypes = 3, // 必须包含 3 种类型
AllowSymbols = true,
AllowWhitespace = false
};
bool isValid = password.ValidatePassword(options);
bool isValid = password.ValidatePassword(options, out string? error);
// 使用自定义规则生成哈希
string? hash = password.ToBCryptIfValid(options, workFactor: 12);
自定义额外验证(如禁用常见密码)
var options = new PasswordValidationOptions
{
CustomValidator = (pwd) =>
{
var commonPasswords = new[] { "12345678", "password", "qwertyui" };
if (commonPasswords.Contains(pwd))
return (false, "密码过于简单,请使用更复杂的密码。");
return (true, string.Empty);
}
};
bool isValid = password.ValidatePassword(options, out string? error);
自定义字符分类(如支持中文)
var options = new PasswordValidationOptions
{
CustomCharTypeClassifier = (ch) =>
{
if (char.IsLetter(ch) || IsChineseChar(ch))
return CharType.Letter;
if (char.IsDigit(ch))
return CharType.Digit;
if (char.IsSymbol(ch) || char.IsPunctuation(ch))
return CharType.Symbol;
return CharType.Other;
}
};
完整 API 一览
BCrypt 哈希与验证
| 方法 | 说明 |
|---|---|
ToBCrypt(int workFactor = 10) |
标准哈希 |
ToBCryptEnhanced(int workFactor = 10) |
增强型哈希 |
VerifyBCrypt(string hash) |
标准验证 |
VerifyBCryptEnhanced(string hash) |
增强型验证 |
VerifyBCrypt(string hash, int expectedWorkFactor, out bool needsRehash) |
验证 + 工作因子升级检测 |
密码验证
| 方法 | 说明 |
|---|---|
ValidatePassword() |
使用默认规则验证密码 |
ValidatePassword(PasswordValidationOptions options) |
使用自定义规则验证密码 |
ValidatePassword(out string? errorMessage) |
验证并返回错误信息(默认规则) |
ValidatePassword(PasswordValidationOptions options, out string? errorMessage) |
验证并返回错误信息(自定义规则) |
验证 + 哈希(组合方法)
| 方法 | 说明 |
|---|---|
ToBCryptIfValid(int workFactor = 10) |
验证通过后生成标准哈希(默认规则) |
ToBCryptIfValid(PasswordValidationOptions options, int workFactor = 10) |
验证通过后生成标准哈希(自定义规则) |
ToBCryptEnhancedIfValid(int workFactor = 10) |
验证通过后生成增强型哈希(默认规则) |
ToBCryptEnhancedIfValid(PasswordValidationOptions options, int workFactor = 10) |
验证通过后生成增强型哈希(自定义规则) |
PasswordValidationOptions 配置项
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
MinLength |
int |
8 |
密码最小长度 |
MaxLength |
int |
20 |
密码最大长度 |
AllowLetters |
bool |
true |
是否允许字母 |
AllowDigits |
bool |
true |
是否允许数字 |
AllowSymbols |
bool |
true |
是否允许符号(标点/符号字符) |
MinCharTypes |
int |
2 |
最少需要的字符类型数量 |
AllowWhitespace |
bool |
false |
是否允许空格 |
CustomCharTypeClassifier |
Func<char, CharType>? |
null |
自定义字符类型分类器 |
CustomValidator |
Func<string, (bool IsValid, string ErrorMessage)>? |
null |
自定义额外验证规则 |
许可证
MIT
| Product | Versions 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 is compatible. 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.
-
net10.0
- BCrypt.Net-Next (>= 4.0.3)
-
net8.0
- BCrypt.Net-Next (>= 4.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.