C# Class BCrypt.Net.BCrypt

BCrypt implementation.

BCrypt implements OpenBSD-style Blowfish password hashing using the scheme described in "A Future- Adaptable Password Scheme" by Niels Provos and David Mazieres.

This password hashing system tries to thwart off-line password cracking using a computationally-intensive hashing algorithm, based on Bruce Schneier's Blowfish cipher. The work factor of the algorithm is parameterised, so it can be increased as computers get faster.

Usage is really simple. To hash a password for the first time, call the method with a random salt, like this:

string pw_hash = BCrypt.HashPassword(plain_password);

To check whether a plaintext password matches one that has been hashed previously, use the Verify method:

if (BCrypt.Verify(candidate_password, stored_hash)) Console.WriteLine("It matches"); else Console.WriteLine("It does not match");

The GenerateSalt() method takes an optional parameter (workFactor) that determines the computational complexity of the hashing:

string strong_salt = BCrypt.GenerateSalt(10); string stronger_salt = BCrypt.GenerateSalt(12);

The amount of work increases exponentially (2^workFactor), so each increment is twice as much work. The default workFactor is 10, and the valid range is 4 to 31.

Datei anzeigen Open project: neoKushan/BCrypt.Net-Core Class Usage Examples

Public Methods

Method Description
GenerateSalt ( ) : string

Generate a salt for use with the BCrypt.HashPassword(string,string) method selecting a reasonable default for the number of hashing rounds to apply.

GenerateSalt ( int workFactor ) : string

Generate a salt for use with the BCrypt.HashPassword(string,string) method.

HashPassword ( string input ) : string

Hash a password using the OpenBSD bcrypt scheme and a salt generated by .

HashPassword ( string input, int workFactor ) : string

Hash a password using the OpenBSD bcrypt scheme and a salt generated by using the given workFactor.

HashPassword ( string input, string salt ) : string

Hash a password using the OpenBSD bcrypt scheme.

HashString ( string source ) : string

Hash a string using the OpenBSD bcrypt scheme and a salt generated by .

Just an alias for HashPassword.

HashString ( string source, int workFactor ) : string

Hash a string using the OpenBSD bcrypt scheme and a salt generated by .

Just an alias for HashPassword.

Verify ( string text, string hash ) : bool

Verifies that the hash of the given text matches the provided hash

Private Methods

Method Description
Char64 ( char character ) : int

Look up the 3 bits base64-encoded by the specified character, range-checking against conversion table.

CryptRaw ( byte inputBytes, byte saltBytes, int logRounds ) : byte[]

Perform the central hashing step in the bcrypt scheme.

DecodeBase64 ( string encodedstring, int maximumBytes ) : byte[]

Decode a string encoded using bcrypt's base64 scheme to a byte array. Note that this is *not* compatible with the standard MIME-base64 encoding.

EKSKey ( byte saltBytes, byte inputBytes ) : void

Perform the "enhanced key schedule" step described by Provos and Mazieres in "A Future- Adaptable Password Scheme" http://www.openbsd.org/papers/bcrypt-paper.ps.

Encipher ( uint blockArray, int offset ) : void

Blowfish encipher a single 64-bit block encoded as two 32-bit halves.

EncodeBase64 ( byte byteArray, int length ) : string

Encode a byte array using bcrypt's slightly-modified base64 encoding scheme. Note that this is *not* compatible with the standard MIME-base64 encoding.

InitializeKey ( ) : void

Initializes the Blowfish key schedule.

Key ( byte keyBytes ) : void

Key the Blowfish cipher.

StreamToWord ( byte data, int &offset ) : uint

Cycically extract a word of key material.

Method Details

GenerateSalt() public static method

Generate a salt for use with the BCrypt.HashPassword(string,string) method selecting a reasonable default for the number of hashing rounds to apply.
public static GenerateSalt ( ) : string
return string

GenerateSalt() public static method

Generate a salt for use with the BCrypt.HashPassword(string,string) method.
public static GenerateSalt ( int workFactor ) : string
workFactor int The log2 of the number of rounds of hashing to apply - the work /// factor therefore increases as 2**workFactor.
return string

HashPassword() public static method

Hash a password using the OpenBSD bcrypt scheme and a salt generated by .
public static HashPassword ( string input ) : string
input string The password to hash.
return string

HashPassword() public static method

Hash a password using the OpenBSD bcrypt scheme and a salt generated by using the given workFactor.
public static HashPassword ( string input, int workFactor ) : string
input string The password to hash.
workFactor int The log2 of the number of rounds of hashing to apply - the work /// factor therefore increases as 2^workFactor.
return string

HashPassword() public static method

Hash a password using the OpenBSD bcrypt scheme.
Thrown when one or more arguments have unsupported or /// illegal values.
public static HashPassword ( string input, string salt ) : string
input string The password to hash.
salt string the salt to hash with (perhaps generated using BCrypt.gensalt).
return string

HashString() public static method

Hash a string using the OpenBSD bcrypt scheme and a salt generated by .
Just an alias for HashPassword.
public static HashString ( string source ) : string
source string The string to hash.
return string

HashString() public static method

Hash a string using the OpenBSD bcrypt scheme and a salt generated by .
Just an alias for HashPassword.
public static HashString ( string source, int workFactor ) : string
source string The string to hash.
workFactor int The log2 of the number of rounds of hashing to apply - the work /// factor therefore increases as 2^workFactor.
return string

Verify() public static method

Verifies that the hash of the given text matches the provided hash
public static Verify ( string text, string hash ) : bool
text string The text to verify.
hash string The previously-hashed password.
return bool