首頁  >  文章  >  後端開發  >  如何在Golang中用Blowfish實作PHP的crypt()函數?

如何在Golang中用Blowfish實作PHP的crypt()函數?

Susan Sarandon
Susan Sarandon原創
2024-10-28 06:32:02380瀏覽

How to Achieve the Functionality of PHP's crypt() with Blowfish in Golang?

PHP 的 crypt() 函數的 Golang 等效項,支援 Blowfish

PHP 的 crypt() 函數提供了一系列雜湊方法,包括 Blowfish。由於缺乏直接等效項,嘗試在 Golang 中複製其行為已被證明具有挑戰性。

替代解決方案

儘管缺乏確切的Golang crypt() 等效項,使用golang.org/x/crypto/bcrypt 套件可以提供有效的替代方案。以下程式碼片段提供了一個範例:

<code class="go">import "golang.org/x/crypto/bcrypt"

// Check whether the bcrypt version of "enter-new-password" matches "a$f5561d2634fb28a969f2dO8QeQ70f4bjCnF/.GvPpjj.8jgmtzZP2".
check := bcrypt.CompareHashAndPassword([]byte("a$f5561d2634fb28a969f2dO8QeQ70f4bjCnF/.GvPpjj.8jgmtzZP2"), []byte("enter-new-password"))
log.Println(check)</code>

如果「enter-new-password」的 bcrypt 雜湊值與提供的雜湊值匹配,則檢查將為 nil。否則,check將被設定為錯誤物件。

注意事項

PHP的crypt()函數支援各種雜湊演算法,例如sha256、sha512和blowfish。在 Golang 中,需要明確指定雜湊類型、成本和其他參數。

提供的雜湊中的 $2a$ 前綴建議使用基於河豚的演算法。一些早期的解決方案嘗試沒有考慮到這一點,從而導致了不正確的結果。

golang.org/x/crypto/bcrypt/bcrypt_test.go 檔案提供了有關如何有效利用此套件的更多範例。

以上是如何在Golang中用Blowfish實作PHP的crypt()函數?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn