使用 Go 根據 /etc/shadow 檔案中的雜湊密碼驗證密碼
php小编子墨今天为大家介绍一种使用Go语言验证密码的方法,这个方法是基于读取/etc/shadow文件中的哈希密码进行验证的。在日常开发中,密码验证是一个非常重要的功能,而使用Go语言可以快速、高效地实现密码验证的功能。通过读取/etc/shadow文件中的哈希密码并进行比对,我们可以确保用户输入的密码与存储在系统中的密码一致。本文将详细介绍这个方法的实现过程,希望能对大家有所帮助。
问题内容
我目前在 /etc/shadow
文件中的密码格式类似于 $6$icnb6xpt8xjwc$ai9rq5hqpep.juts/tubhk/oi7so/s1aa.ihgbjhn12qmt5p44x5or86pso9/opbo4cmo0at4xumc0ycapo8 7/
(sha512
加密密码)。我需要做的是验证密码(用户提供的密码和当前的哈希密码)。我正在 go
上实现这个。
我试图实现这一目标的方法是,获取用户提供的密码,使用与 /etc/shadow
中相同的盐对其进行哈希处理,并检查它们是否相似或不同。如何生成相同的哈希值并验证密码?
下面是我正在做的粗略代码(根据 amadan 对编码的评论进行了更新)
// this is what is stored on /etc/shadow - a hashed string of "test" myPwd := "$6$IcnB6XpT8xjWC$AI9Rq5hqpEP.Juts/TUbHk/OI7sO/S1AA.ihgBjHN12QmT5p44X5or86PsO9/oPBO4cmo0At4XuMC0yCApo87/" // getting the salt salt := strings.Split(myPwd, "$")[2] encoding := base64.NewEncoding("./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz").WithPadding(base64.NoPadding) decodedSalt, err := encoding.DecodeString(salt) // user provided password myNewPwd := "test" newHashedPwd, err := hashPwd512(string(myNewPwd), string(decodedSalt)) // comparision if (newHashedPwd == myPwd) { // password is same, validate it } // What I'm expecting from this method is that for the same password stored in the /etc/shadow, // and the same salt, it should return (like the one in /etc/shadow file) // AI9Rq5hqpEP.Juts/TUbHk/OI7sO/S1AA.ihgBjHN12QmT5p44X5or86PsO9/oPBO4cmo0At4XuMC0yCApo87/ func hashPwd512(pwd string, salt string) (string, error) { hash := sha512.New() hash.Write([]byte(salt)) hash.Write([]byte(pwd)) encoding := base64.NewEncoding("./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz").WithPadding(base64.NoPadding) hashedPwd := encoding.EncodeToString(hash.Sum(nil)) return hashedPwd, nil }
注意:密码通过passwd
或chpasswd
设置/更改。
解决方法
sha512 是一种快速哈希。这对密码来说是不利的,因为当每次尝试几乎不需要花费任何成本时,暴力破解就变得非常容易。为了减慢速度,/etc/shadow
中的内容不仅仅是一个简单的 sha512 哈希值,而是 key拉伸适用于哈希算法运行数千次的情况。具体的密钥拉伸算法好像是这个。因此,crypto/sha512
只完成了所需任务的大约 1/5000(默认情况下)。
package main import ( "fmt" "strings" "github.com/gehirninc/crypt" _ "github.com/gehirninc/crypt/sha512_crypt" ) func main() { saltedpass := "$6$icnb6xpt8xjwc$ai9rq5hqpep.juts/tubhk/oi7so/s1aa.ihgbjhn12qmt5p44x5or86pso9/opbo4cmo0at4xumc0ycapo87/" fmt.println("original: ", saltedpass) // make new hash from scratch plainpass := "test" crypt := crypt.sha512.new() newsaltedpass, err := crypt.generate([]byte(plainpass), []byte(saltedpass)) if err != nil { panic(err) } fmt.println("generated:", newsaltedpass) // verify a password (correct) err = crypt.verify(saltedpass, []byte(plainpass)) fmt.println("verification error (correct password): ", err) // verify a password (incorrect) badpass := "fail" err = crypt.verify(saltedpass, []byte(badpass)) fmt.println("verification error (incorrect password):", err) }
由于您只需要验证,因此 verify
快捷方式就足够了(它会在幕后为您生成 generate
):
err = crypt.verify(saltedpass, []byte(plainpass)) if err == nil { fmt.println("fly, you fools!") } else { fmt.println("you shall not pass!") }
输出:
Original: $6$IcnB6XpT8xjWC$AI9Rq5hqpEP.Juts/TUbHk/OI7sO/S1AA.ihgBjHN12QmT5p44X5or86PsO9/oPBO4cmo0At4XuMC0yCApo87/ Generated: $6$IcnB6XpT8xjWC$AI9Rq5hqpEP.Juts/TUbHk/OI7sO/S1AA.ihgBjHN12QmT5p44X5or86PsO9/oPBO4cmo0At4XuMC0yCApo87/ Verification error (correct password): <nil> Verification error (incorrect password): hashed value is not the hash of the given password
注意:我相信我的评论是错误的。密码哈希本身就是通过这种编码进行编码的,但盐似乎是真正的盐(只是当它随机生成时,使用了该编码中的字符)。
该库还支持其他哈希函数,但您确实需要为每个函数进行导入才能注册它。您还可以看到,我没有费心从 saltedpass
中分离盐;这也是您不需要担心的事情。
但是,如果您出于某种原因确实想隔离盐,那么还要注意,从一开始就计算 $
并不是一个安全的想法,因为它将无法处理像 $6$rounds=77777$ 这样的条目例如,正确地短 $wuqyw2yr.hbnpjjrhpyd/ifiw05xdfeeyqomxixbkvr0g
。使用 strings.lastindex(saltedpass, "$") + 1
作为切入点。
以上是使用 Go 根據 /etc/shadow 檔案中的雜湊密碼驗證密碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

GoLand中自定義結構體標籤不顯示怎麼辦?在使用GoLand進行Go語言開發時,很多開發者會遇到自定義結構體標籤在�...

Go語言中用於浮點數運算的庫介紹在Go語言(也稱為Golang)中,進行浮點數的加減乘除運算時,如何確保精度是�...

Go爬蟲Colly中的Queue線程問題探討在使用Go語言的Colly爬蟲庫時,開發者常常會遇到關於線程和請求隊列的問題。 �...

Go語言中使用RedisStream實現消息隊列時類型轉換問題在使用Go語言與Redis...

Go語言中字符串打印的區別:使用Println與string()函數的效果差異在Go...

Go語言中結構體定義的兩種方式:var與type關鍵字的差異Go語言在定義結構體時,經常會看到兩種不同的寫法:一�...

Go語言中哪些庫是大公司開發或知名開源項目?在使用Go語言進行編程時,開發者常常會遇到一些常見的需求,�...

Go編程中的資源管理:Mysql和Redis的連接與釋放在學習Go編程過程中,如何正確管理資源,特別是與數據庫和緩存�...
