保護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對於更簡單的哈希需求,可以使用 PBKDF2:
1. Install the package: go get golang.org/x/crypto/pbkdf21.安裝包: 去取得 golang.org/x/crypto/pbkdf2
2。用法範例:
包主
進口 (
“FMMT”
“golang.org/x/crypto/pbkdf2”
)
函數主() {
通過 := []byte("你的密碼")
鹽 := []byte("你的鹽")
// 產生哈希值
哈希 := pbkdf2.Key(pass, salt, 4096, sha256.Size, sha256.New)
fmt.Printf("%x\n", hash) // 範例輸出:0x079b8238d3815d31d87d75ff893371ac3cc875f97eca499854655da9554d25555
}
免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。
Copyright© 2022 湘ICP备2022001581号-3