GoLang password_hash

is this article helpful? yes | no
GoLang replacement for PHP's password_hash [Golang Play | edit | history]
package main

import (
func PasswordHash(password string) (string, error) {
	bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
	return string(bytes), err
func PasswordVerify(password, hash string) bool {
	err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
	return err == nil
func main() {
	password := "123456"
	hash, _ := PasswordHash(password)
	fmt.Println("password:", password)
	fmt.Println("hash:", hash)
	match := PasswordVerify(password, hash)
	 fmt.Println("Verify:", match)

Source: https://www.programmersought.com/article/53551785934/

PHP password_hash

PHP original manual for password_hash [ show | php.net ]


(PHP 5 >= 5.5.0, PHP 7)

password_hashCreates a password hash


string password_hash ( string $password , int $algo [, array $options ] )

password_hash() creates a new password hash using a strong one-way hashing algorithm. password_hash() is compatible with crypt(). Therefore, password hashes created by crypt() can be used with password_hash().

The following algorithms are currently supported:

  • PASSWORD_DEFAULT - Use the bcrypt algorithm (default as of PHP 5.5.0). Note that this constant is designed to change over time as new and stronger algorithms are added to PHP. For that reason, the length of the result from using this identifier can change over time. Therefore, it is recommended to store the result in a database column that can expand beyond 60 characters (255 characters would be a good choice).
  • PASSWORD_BCRYPT - Use the CRYPT_BLOWFISH algorithm to create the hash. This will produce a standard crypt() compatible hash using the "$2y$" identifier. The result will always be a 60 character string, or FALSE on failure.
  • PASSWORD_ARGON2I - Use the Argon2 hashing algorithm to create the hash.

Supported options for PASSWORD_BCRYPT:

  • salt (string) - to manually provide a salt to use when hashing the password. Note that this will override and prevent a salt from being automatically generated.

    If omitted, a random salt will be generated by password_hash() for each password hashed. This is the intended mode of operation.


    The salt option has been deprecated as of PHP 7.0.0. It is now preferred to simply use the salt that is generated by default.

  • cost (integer) - which denotes the algorithmic cost that should be used. Examples of these values can be found on the crypt() page.

    If omitted, a default value of 10 will be used. This is a good baseline cost, but you may want to consider increasing it depending on your hardware.

Supported options for PASSWORD_ARGON2I:

  • memory_cost (integer) - Maximum memory (in bytes) that may be used to compute the Argon2 hash. Defaults to PASSWORD_ARGON2_DEFAULT_MEMORY_COST.

  • time_cost (integer) - Maximum amount of time it may take to compute the Argon2 hash. Defaults to PASSWORD_ARGON2_DEFAULT_TIME_COST.

  • threads (integer) - Number of threads to use for computing the Argon2 hash. Defaults to PASSWORD_ARGON2_DEFAULT_THREADS.



The user's password.


Using the PASSWORD_BCRYPT as the algorithm, will result in the password parameter being truncated to a maximum length of 72 characters.


A password algorithm constant denoting the algorithm to use when hashing the password.


An associative array containing options. See the password algorithm constants for documentation on the supported options for each algorithm.

If omitted, a random salt will be created and the default cost will be used.

Return Values

Returns the hashed password, or FALSE on failure.

The used algorithm, cost and salt are returned as part of the hash. Therefore, all information that's needed to verify the hash is included in it. This allows the password_verify() function to verify the hash without needing separate storage for the salt or algorithm information.


Example #1 password_hash() example

 * We just want to hash our password using the current DEFAULT algorithm.
 * This is presently BCRYPT, and will produce a 60 character result.
 * Beware that DEFAULT may change over time, so you would want to prepare
 * By allowing your storage to expand past 60 characters (255 would be good)
echo password_hash("rasmuslerdorf"PASSWORD_DEFAULT);

The above example will output something similar to:


Example #2 password_hash() example setting cost manually

 * In this case, we want to increase the default cost for BCRYPT to 12.
 * Note that we also switched to BCRYPT, which will always be 60 characters.
$options = [
'cost' => 12,

The above example will output something similar to:


Example #3 password_hash() example setting salt manually

 * Note that the salt here is randomly generated.
 * Never use a static salt or one that is not randomly generated.
 * For the VAST majority of use-cases, let password_hash generate the salt randomly for you
$options = [
'cost' => 11,
'salt' => mcrypt_create_iv(22MCRYPT_DEV_URANDOM),

The above example will output something similar to:


Example #4 password_hash() example finding a good cost

 * This code will benchmark your server to determine how high of a cost you can
 * afford. You want to set the highest cost that you can without slowing down
 * you server too much. 8-10 is a good baseline, and more is good if your servers
 * are fast enough. The code below aims for â‰¤ 50 milliseconds stretching time,
 * which is a good baseline for systems handling interactive logins.
$timeTarget 0.05// 50 milliseconds 

$cost 8;
do {
$start microtime(true);
password_hash("test"PASSWORD_BCRYPT, ["cost" => $cost]);
$end microtime(true);
} while ((
$end $start) < $timeTarget);

"Appropriate Cost Found: " $cost;

The above example will output something similar to:

Appropriate Cost Found: 10

Example #5 password_hash() example using Argon2

echo 'Argon2 hash: ' password_hash('rasmuslerdorf'PASSWORD_ARGON2I);

The above example will output something similar to:

Argon2 hash: $argon2i$v=19$m=1024,t=2,p=2$YzJBSzV4TUhkMzc3d3laeg$zqU/1IN0/AogfP4cmSJI1vc8lpXRW9/S0sYY2i2jHT0



It is strongly recommended that you do not generate your own salt for this function. It will create a secure salt automatically for you if you do not specify one.

As noted above, providing the salt option in PHP 7.0 will generate a deprecation warning. Support for providing a salt manually may be removed in a future PHP release.


It is recommended that you test this function on your servers, and adjust the cost parameter so that execution of the function takes less than 100 milliseconds on interactive systems. The script in the above example will help you choose a good cost value for your hardware.

Note: Updates to supported algorithms by this function (or changes to the default one) must follow the following rules:

  • Any new algorithm must be in core for at least 1 full release of PHP prior to becoming default. So if, for example, a new algorithm is added in 7.5.5, it would not be eligible for default until 7.7 (since 7.6 would be the first full release). But if a different algorithm was added in 7.6.0, it would also be eligible for default at 7.7.0.
  • The default should only change in a full release (7.3.0, 8.0.0, etc) and not in a revision release. The only exception to this is in an emergency when a critical security flaw is found in the current default.


Version Description
7.2.0 Support for Argon2 passwords using PASSWORD_ARGON2I was added.

See Also