Skip to content
lassir edited this page Nov 25, 2014 · 6 revisions

Functions

bcrypt_hash

Description: Calculate a bcrypt hash for a given string, using the given work factor.

Parameters:

  • key[]: The string to calculate the hash for
  • cost: The work factor to be used
  • callback_name: The name of the function that's called when the hash has been computed
  • callback_format: The format of the callback's parameters
  • {Float, _}:...: Any additional parameters to be passed to the callback

Return values: 1 on success, 0 on failure.

Example:

new password[] = "Hello World!";
bcrypt_hash(password, 12, "OnPasswordHashed", "d", playerid);

bcrypt_get_hash

Description: Retrieve the calculated bcrypt hash. This function must be called within the callback defined in bcrypt_hash.

Parameters:

  • dest[]: The destination array to save the hash to

Return values: 1 on success, 0 on failure.

Example:

public OnPasswordHashed(playerid)
{
	new hash[BCRYPT_HASH_LENGTH];
	bcrypt_get_hash(hash);

	printf("Hash: %s", hash);
	return 1;
}

bcrypt_check

Description: Check if a given string matches a given bcrypt hash.

Parameters:

  • key[]: The string to check
  • hash[]: The hash to check the string against
  • callback_name[]: The name of the function that's called when the key has been checked
  • callback_format[]: The format of the callback's parameters
  • {Float, _}:...: Any additional parameters to be passed to the callback

Return values: 1 on success, 0 on failure.

Example:

new password[] = "Hello World!";
new key[] = "$2y$12$33T1WbJGYD9YVKpBShTDsOOlS3248tApLCndjz28n0cyWZR1HYXy6";
bcrypt_check(password, key, "OnPasswordChecked", "d", playerid);

bcrypt_is_equal

Description: Retrieve the result of bcrypt_verify.

Parameters:

  • (none)

Return values: True if the password matches the hash, otherwise false.

Example:

public OnPasswordChecked(playerid)
{
	new bool:match = bcrypt_is_equal();
	if(match)
	{
		print("The password matches!");
	}
	else
	{
		print("The password doesn't match!");
	}
	return 1;
}

bcrypt_needs_rehash

Description: Check if a given hash is outdated, and therefore needs to be updated. This could be due to a change in the used work factor.

Parameters:

  • hash[]: The hash you wish to see if it is up-to-date
  • cost: The expected cost (work factor)

Return values: 1 if the password is outdated and needs to be rehashed, otherwise 0.

Example:

new key[] = "$2y$12$33T1WbJGYD9YVKpBShTDsOOlS3248tApLCndjz28n0cyWZR1HYXy6";
new bool:outdated = bcrypt_needs_rehash(password, 13);
if(outdated)
{
	print("The hash needs to be updated.");
}

bcrypt_find_cost

Description: Find a cost nearest to a given time target in milliseconds.

Parameters:

  • time_target: The desired time for the hashing to take (default: 250)

Return values: The cost nearest to the time target.

Example:

public OnGameModeInit()
{
	// The result will be printed on the console and written to the server log
	// The function also returns the cost as an integer, but more detailed 
	// results are printed on the log.
	bcrypt_find_cost();
	return 1;
}

Example output:

plugin.bcrypt: Calculating appropriate cost for time target 250 ms...
plugin.bcrypt: Cost 11: 144 ms (-106 ms)
plugin.bcrypt: Cost 12: 292 ms (+42 ms)
plugin.bcrypt: => Best match is cost 12.

bcrypt_set_thread_limit

Description: Set the maximum number of concurrent threads used by the plugin.

Parameters:

  • value: The maximum number of theads to use

Return values: 1 on success, 0 on failure.

Example:

public OnGameModeInit()
{
	// Say the CPU has 8 threads, but we only want to use 3 concurrent threads at most
	bcrypt_set_thread_limit(3);
	return 1;
}

bcrypt_debug

Description: Set the debugging level of the plugin.

Parameters:

  • level: The verbosity of the log.

Allowed values:

BCRYPT_LOG_ERROR, 
BCRYPT_LOG_WARNING, 
BCRYPT_LOG_INFO, 
BCRYPT_LOG_DEBUG, 
BCRYPT_LOG_TRACE

Return values: 1 on success, 0 on failure.

Example:

public OnGameModeInit()
{
	// Set the debugging level to log warnings
	// Please note that BCRYPT_LOG_ERROR is the recommended value on production servers
	bcrypt_debug(BCRYPT_LOG_WARNING);
	return 1;
}