Home >Backend Development >Golang >Is There a True Go Equivalent to PHP\'s crypt() Function?
Crypt function in PHP and Golang
Problem Introduction
The PHP crypt() function generates a hashed value of a given string using a specified algorithm. A recent question arose seeking a Golang equivalent of this function that would produce identical results for specific input and hash values.
Unsuccessful Attempts
Several attempts were made to implement the Go equivalent of PHP's crypt() function:
Solution
Despite not finding an exact "Go crypt function" equivalent, an alternative was discovered:
Example
The following Go code can be used:
<code class="go">import "golang.org/x/crypto/bcrypt" check := bcrypt.CompareHashAndPassword([]byte("a$f5561d2634fb28a969f2dO8QeQ70f4bjCnF/.GvPpjj.8jgmtzZP2"), []byte("enter-new-password")) log.Println(check)</code>
This code will return nil if the bcrypt version of "enter-new-password" is the same as the provided hash value; otherwise, it will return an error.
Additional Notes
The above is the detailed content of Is There a True Go Equivalent to PHP\'s crypt() Function?. For more information, please follow other related articles on the PHP Chinese website!