commonlib
BCrypt Class Reference

Wrapper class for BCrypt password hashing algorithm. More...

#include <bcrypt.h>

Public Member Functions

std::string hash (const std::string &password, unsigned int rounds=10)
 Compute BCrypt hash of a password using random salt. More...
 
std::string hash (const std::string &password, std::string salt, unsigned int rounds=10) const
 Compute BCrypt hash of a password using static salt. More...
 
std::string generateSalt (unsigned int size)
 Generate a random salt string of the specified size. More...
 
std::string generateSalt (char *bytes, int size) const
 Generate a salt string from provided byte array. More...
 

Static Public Member Functions

static bool Verify (const std::string &hash, const std::string &password)
 Verifies if the password matches a hash. More...
 

Detailed Description

Wrapper class for BCrypt password hashing algorithm.

Member Function Documentation

◆ generateSalt() [1/2]

std::string BCrypt::generateSalt ( unsigned int  size)

Generate a random salt string of the specified size.

Parameters
sizeThe size of the salt to be generated
Returns
Generated salt string
Exceptions
{BCryptException}if blowfish algorithm failed to generate the salt
{BCryptSaltSizeException}if size is too small

◆ generateSalt() [2/2]

std::string BCrypt::generateSalt ( char *  bytes,
int  size 
) const

Generate a salt string from provided byte array.

Parameters
bytesByte array from which to generate the salt
sizeThe size of the provided byte arry
Returns
Generated salt string
Exceptions
{BCryptException}if blowfish algorithm failed to generate the salt
{BCryptSaltSizeException}if size is too small

◆ hash() [1/2]

std::string BCrypt::hash ( const std::string &  password,
unsigned int  rounds = 10 
)

Compute BCrypt hash of a password using random salt.

Parameters
passwordThe password in plain text to be hashed
roundsBase-2 logarithm of the iteration count for the underlying Blowfish-based hashing algorithm
Returns
Hash string of the provided password
Exceptions
{BCryptException}if blowfish algorithm failed to generate the hash

◆ hash() [2/2]

std::string BCrypt::hash ( const std::string &  password,
std::string  salt,
unsigned int  rounds = 10 
) const

Compute BCrypt hash of a password using static salt.

Try to avoid using this method, but if you must have a static salt, then generate it with generateSalt(int size) method.

Parameters
passwordThe password in plain text to be hashed
saltThe salt to be used for hashing the password
roundsBase-2 logarithm of the iteration count for the underlying Blowfish-based hashing algorithm
Returns
Hash string of the provided password
Exceptions
{BCryptException}if blowfish algorithm failed to generate the hash
{BCryptSaltSizeException}if salt size is too small

◆ Verify()

static bool BCrypt::Verify ( const std::string &  hash,
const std::string &  password 
)
static

Verifies if the password matches a hash.

Parameters
hashBCrypt hash of the password
passwordThe password in plain text
Returns
true if password matches the hash

The documentation for this class was generated from the following file: