保护 Go on App Engine 中的用户密码
在 Google App Engine 上部署的 Go 应用程序中处理用户密码时,安全性至关重要。 bcrypt 库虽然在密码散列方面有效,但由于它使用系统调用而带来了限制。因此,开发人员可能会寻求安全密码散列的替代方法。
一个可靠的选择是利用 golang.org/x/crypto 包,它提供了 PBKDF2 和 bcrypt 的本机实现。这些实现消除了对系统调用的依赖,使其适合 App Engine。
使用 bcrypt
要使用 bcrypt,请按照以下步骤操作:
1. Install the package: go get golang.org/x/crypto/bcrypt
2. Example usage: package main import ( "fmt" "golang.org/x/crypto/bcrypt" ) func main() { pass := []byte("your password") // Generate a hashed password ctext, err := bcrypt.GenerateFromPassword(pass, bcrypt.DefaultCost) if err != nil { // Handle error } fmt.Println(string(ctext)) // Example output: $2a$10$sylGijT5CIJZ9ViJsxZOS.IB2tOtJ40hf82eFbTwq87iVAOb5GL8e }
使用 PBKDF2
对于更简单的哈希需求,可以使用 PBKDF2:
1. Install the package: go get golang.org/x/crypto/pbkdf2
2. Example usage: package main import ( "fmt" "golang.org/x/crypto/pbkdf2" ) func main() { pass := []byte("your password") salt := []byte("your salt") // Generate a hash hash := pbkdf2.Key(pass, salt, 4096, sha256.Size, sha256.New) fmt.Printf("%x\n", hash) // Example output: 0x079b8238d3815d31d87d75ff893371ac3cc875f97eca499854655da9554d2555 }
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3