Bcrypt Class
Provides helper methods to hash and verify passwords using BCrypt.
public static class BcryptRemarks
BCrypt hashes are encoded as modular crypt strings such as $2b$12$... where the revision is one of 2, 2a, 2b, 2x, or 2y, and the work factor (cost) is in the range 4 to 31.
Per the BCrypt specification, input is processed as UTF-8 and only the first 72 bytes are used.
Methods
| Name | Description |
|---|---|
| GenerateSalt(int, BcryptVersion) | Generates a BCrypt salt for the specified work factor and revision. |
| HashPassword(ReadOnlySpan<char>, int, BcryptVersion) | Hashes a password using BCrypt and a generated salt. |
| HashPassword(ReadOnlySpan<char>, ReadOnlySpan<char>) | Hashes a password using a specific BCrypt salt. |
| HashPassword(string, int, BcryptVersion) | Hashes a password using BCrypt and a generated salt. |
| HashPassword(string, string) | Hashes a password using a specific BCrypt salt. |
| NeedsRehash(ReadOnlySpan<char>, int, BcryptVersion) | Determines whether an existing hash should be rehashed using new settings. |
| NeedsRehash(string, int, BcryptVersion) | Determines whether an existing hash should be rehashed using new settings. |
| ParseHash(ReadOnlySpan<char>) | Parses a BCrypt hash and returns its revision and work factor. |
| ParseHash(string) | Parses a BCrypt hash and returns its revision and work factor. |
| TryParseHash(ReadOnlySpan<char>, BcryptHashInfo) | Attempts to parse a BCrypt hash and extract its revision and work factor. |
| TryParseHash(string?, BcryptHashInfo) | Attempts to parse a BCrypt hash and extract its revision and work factor. |
| Verify(ReadOnlySpan<char>, ReadOnlySpan<char>) | Verifies that a password matches a BCrypt hash. |
| Verify(string, string) | Verifies that a password matches a BCrypt hash. |
GenerateSalt(int, BcryptVersion)
Bcrypt.csGenerates a BCrypt salt for the specified work factor and revision.
public static string GenerateSalt(int workFactor = 11, BcryptVersion version = BcryptVersion.Revision2B)Parameters
workFactorint- The BCrypt work factor. Must be between
Bcrypt.MinWorkFactorandBcrypt.MaxWorkFactor. versionBcryptVersion- The BCrypt revision to generate.
Returns
string A BCrypt salt string.
Exceptions
ArgumentOutOfRangeExceptionworkFactoris outside the supported range.NotSupportedExceptionversionisBcryptVersion.Revision2orBcryptVersion.Revision2X, neither of which can be generated.
HashPassword overloads
HashPassword(ReadOnlySpan<char>, int, BcryptVersion)
Bcrypt.csHashes a password using BCrypt and a generated salt.
public static string HashPassword(ReadOnlySpan<char> password, int workFactor = 11, BcryptVersion version = BcryptVersion.Revision2B)Parameters
passwordReadOnlySpan<char>- The password to hash.
workFactorint- The BCrypt work factor. Must be between
Bcrypt.MinWorkFactorandBcrypt.MaxWorkFactor. versionBcryptVersion- The BCrypt revision to generate.
Returns
string The BCrypt hash string.
Exceptions
ArgumentExceptionpasswordcannot be encoded as UTF-8 because it contains an unpaired surrogate.
HashPassword(ReadOnlySpan<char>, ReadOnlySpan<char>)
Bcrypt.csHashes a password using a specific BCrypt salt.
public static string HashPassword(ReadOnlySpan<char> password, ReadOnlySpan<char> salt)Parameters
passwordReadOnlySpan<char>- The password to hash.
saltReadOnlySpan<char>- The BCrypt salt string.
Returns
string The BCrypt hash string.
Exceptions
ArgumentExceptionpasswordcannot be encoded as UTF-8 because it contains an unpaired surrogate.FormatExceptionsaltis not a valid BCrypt salt: bad revision, a work factor outsideBcrypt.MinWorkFactor..Bcrypt.MaxWorkFactor, or a malformed payload.
HashPassword(string, int, BcryptVersion)
Bcrypt.csHashes a password using BCrypt and a generated salt.
public static string HashPassword(string password, int workFactor = 11, BcryptVersion version = BcryptVersion.Revision2B)Parameters
passwordstring- The password to hash.
workFactorint- The BCrypt work factor. Must be between
Bcrypt.MinWorkFactorandBcrypt.MaxWorkFactor. versionBcryptVersion- The BCrypt revision to generate.
Returns
string The BCrypt hash string.
Exceptions
ArgumentExceptionpasswordcannot be encoded as UTF-8 because it contains an unpaired surrogate.
HashPassword(string, string)
Bcrypt.csHashes a password using a specific BCrypt salt.
Parameters
Returns
string The BCrypt hash string.
Exceptions
ArgumentExceptionpasswordcannot be encoded as UTF-8 because it contains an unpaired surrogate.FormatExceptionsaltis not a valid BCrypt salt: bad revision, a work factor outsideBcrypt.MinWorkFactor..Bcrypt.MaxWorkFactor, or a malformed payload.
NeedsRehash overloads
NeedsRehash(ReadOnlySpan<char>, int, BcryptVersion)
Bcrypt.csDetermines whether an existing hash should be rehashed using new settings.
public static bool NeedsRehash(ReadOnlySpan<char> hash, int workFactor = 11, BcryptVersion version = BcryptVersion.Revision2B)Parameters
hashReadOnlySpan<char>- The existing BCrypt hash.
workFactorint- The desired work factor.
versionBcryptVersion- The desired BCrypt revision.
Returns
bool true if the hash should be replaced; otherwise, false.
Exceptions
FormatException- The hash is not a valid BCrypt hash format.
NeedsRehash(string, int, BcryptVersion)
Bcrypt.csDetermines whether an existing hash should be rehashed using new settings.
public static bool NeedsRehash(string hash, int workFactor = 11, BcryptVersion version = BcryptVersion.Revision2B)Parameters
hashstring- The existing BCrypt hash.
workFactorint- The desired work factor.
versionBcryptVersion- The desired BCrypt revision.
Returns
bool true if the hash should be replaced; otherwise, false.
Exceptions
FormatException- The hash is not a valid BCrypt hash format.
ParseHash overloads
ParseHash(ReadOnlySpan<char>)
Bcrypt.csParses a BCrypt hash and returns its revision and work factor.
public static BcryptHashInfo ParseHash(ReadOnlySpan<char> hash)Parameters
hashReadOnlySpan<char>- The BCrypt hash string.
Returns
BcryptHashInfo The parsed hash information.
Exceptions
FormatException- The hash is not a valid BCrypt hash format.
ParseHash(string)
Bcrypt.csParses a BCrypt hash and returns its revision and work factor.
public static BcryptHashInfo ParseHash(string hash)Parameters
hashstring- The BCrypt hash string.
Returns
BcryptHashInfo The parsed hash information.
Exceptions
FormatException- The hash is not a valid BCrypt hash format.
TryParseHash overloads
TryParseHash(ReadOnlySpan<char>, BcryptHashInfo)
Bcrypt.csAttempts to parse a BCrypt hash and extract its revision and work factor.
public static bool TryParseHash(ReadOnlySpan<char> hash, out BcryptHashInfo result)Parameters
hashReadOnlySpan<char>- The BCrypt hash string.
resultBcryptHashInfo- When this method returns, contains parsed hash information if parsing succeeded.
Returns
bool true if parsing succeeded; otherwise, false.
TryParseHash(string?, BcryptHashInfo)
Bcrypt.csAttempts to parse a BCrypt hash and extract its revision and work factor.
public static bool TryParseHash(string? hash, out BcryptHashInfo result)Parameters
hashstring?- The BCrypt hash string.
resultBcryptHashInfo- When this method returns, contains parsed hash information if parsing succeeded.
Returns
bool true if parsing succeeded; otherwise, false.
Verify overloads
Verify(ReadOnlySpan<char>, ReadOnlySpan<char>)
Bcrypt.csVerifies that a password matches a BCrypt hash.
public static bool Verify(ReadOnlySpan<char> password, ReadOnlySpan<char> hash)Parameters
passwordReadOnlySpan<char>- The plaintext password.
hashReadOnlySpan<char>- The BCrypt hash string.
Returns
bool true if the password matches; otherwise, false.
Exceptions
NotSupportedExceptionhashuses the$2x$revision, which cannot be verified correctly.
Remarks
A password that cannot be encoded as UTF-8 reports a mismatch rather than throwing.
Verify(string, string)
Bcrypt.csVerifies that a password matches a BCrypt hash.
Parameters
Returns
bool true if the password matches; otherwise, false.
Exceptions
NotSupportedExceptionhashuses the$2x$revision, which cannot be verified correctly.
Remarks
A password that cannot be encoded as UTF-8 reports a mismatch rather than throwing.
Constants
| Name | Description |
|---|---|
| DefaultWorkFactor | Default BCrypt work factor. |
| MaxPasswordLengthInBytes | Maximum number of UTF-8 bytes used by BCrypt for a password. |
| MaxWorkFactor | Maximum BCrypt work factor (cost). |
| MinWorkFactor | Minimum BCrypt work factor (cost). |
DefaultWorkFactor
Default BCrypt work factor.
public const int DefaultWorkFactor = 11MaxPasswordLengthInBytes
Maximum number of UTF-8 bytes used by BCrypt for a password.
public const int MaxPasswordLengthInBytes = 72MaxWorkFactor
Maximum BCrypt work factor (cost).
public const int MaxWorkFactor = 31MinWorkFactor
Minimum BCrypt work factor (cost).
public const int MinWorkFactor = 4