Bcrypt Password Hashing in Golang: Achieving Compatibility with Node.js
Migrating authentication systems from Node.js to Golang requires addressing password hashing compatibility. This article presents a solution using Node.js bcrypt and its equivalent in Golang, golang.org/x/crypto/bcrypt.
To replicate the hashed string generated by Node.js bcrypt in Golang, use the following code:
hashedPassword, err := bcrypt.GenerateFromPassword(password, bcrypt.DefaultCost)
Here's a working example:
package main import ( "golang.org/x/crypto/bcrypt" "fmt" ) func main() { password := []byte("MyDarkSecret") hashedPassword, err := bcrypt.GenerateFromPassword(password, bcrypt.DefaultCost) if err != nil { panic(err) } fmt.Println(string(hashedPassword)) err = bcrypt.CompareHashAndPassword(hashedPassword, password) fmt.Println(err) // nil means it is a match }
By utilizing golang.org/x/crypto/bcrypt, you can ensure compatibility with your existing authentication system while migrating to Golang.
The above is the detailed content of How Can I Ensure Bcrypt Password Hashing Compatibility Between Node.js and Golang?. For more information, please follow other related articles on the PHP Chinese website!