
第一段引用上面的摘要:
本文旨在帮助开发者排查和解决 Go 语言密码认证库中 crypto 包多次调用返回不同结果的问题。通过分析问题代码,找出 hash 函数参数顺序错误,并提供修改建议,确保密码认证的正确性。本文适合对 Go 语言和密码学有一定了解的开发者阅读。
在开发密码认证库时,经常会遇到多次调用加密函数,但结果不一致的问题。这会导致验证失败,影响系统的安全性。本文将以一个实际案例为例,分析问题原因,并提供解决方案。
问题分析
以下代码展示了一个密码认证库的实现,包含 Check() 和 New() 两个函数,分别用于验证密码和生成新的盐值及哈希值。
package mainimport ( "code.google.com/p/go.crypto/scrypt" "crypto/hmac" "crypto/rand" "crypto/sha256" "crypto/subtle" "errors" "fmt" "io")// 常量定义const ( KEYLENGTH = 32 N = 16384 R = 8 P = 1)// hash 函数:使用 scrypt 进行密钥扩展,然后使用 HMAC 生成哈希值func hash(hmk, pw, s []byte) (h []byte, err error) { sch, err := scrypt.Key(pw, s, N, R, P, KEYLENGTH) if err != nil { return nil, err } hmh := hmac.New(sha256.New, hmk) hmh.Write(sch) h = hmh.Sum(nil) hmh.Reset() // 清空 HMAC,可选 return h, nil}// Check 函数:验证密码是否正确func Check(hmk, h, pw, s []byte) (chk bool, err error) { fmt.Printf("Hash: %xnHMAC: %xnSalt: %xnPass: %xn", h, hmk, s, []byte(pw)) hchk, err := hash(hmk, pw, s) if err != nil { return false, err } fmt.Printf("Hchk: %xn", hchk) if subtle.ConstantTimeCompare(h, hchk) != 1 { return false, errors.New("Error: Hash verification failed") } return true, nil}// New 函数:生成新的盐值和哈希值func New(hmk, pw []byte) (h, s []byte, err error) { s = make([]byte, KEYLENGTH) _, err = io.ReadFull(rand.Reader, s) if err != nil { return nil, nil, err } h, err = hash(pw, hmk, s) if err != nil { return nil, nil, err } fmt.Printf("Hash: %xnSalt: %xnPass: %xn", h, s, []byte(pw)) return h, s, nil}func main() { // 已知的有效值 pass := "pleaseletmein" hash := []byte{ 0x6f, 0x38, 0x7b, 0x9c, 0xe3, 0x9d, 0x9, 0xff, 0x6b, 0x1c, 0xc, 0xb5, 0x1, 0x67, 0x1d, 0x11, 0x8f, 0x72, 0x78, 0x85, 0xca, 0x6, 0x50, 0xd0, 0xe6, 0x8b, 0x12, 0x9c, 0x9d, 0xf4, 0xcb, 0x29, } salt := []byte{ 0x77, 0xd6, 0x57, 0x62, 0x38, 0x65, 0x7b, 0x20, 0x3b, 0x19, 0xca, 0x42, 0xc1, 0x8a, 0x4, 0x97, 0x48, 0x44, 0xe3, 0x7, 0x4a, 0xe8, 0xdf, 0xdf, 0xfa, 0x3f, 0xed, 0xe2, 0x14, 0x42, 0xfc, 0xd0, } hmac := []byte{ 0x70, 0x23, 0xbd, 0xcb, 0x3a, 0xfd, 0x73, 0x48, 0x46, 0x1c, 0x6, 0xcd, 0x81, 0xfd, 0x38, 0xeb, 0xfd, 0xa8, 0xfb, 0xba, 0x90, 0x4f, 0x8e, 0x3e, 0xa9, 0xb5, 0x43, 0xf6, 0x54, 0x5d, 0xa1, 0xf2, } // 验证已知值,成功 fmt.Println("Checking known values...") chk, err := Check(hmac, hash, []byte(pass), salt) if err != nil { fmt.Printf("%sn", err) } fmt.Printf("%tn", chk) fmt.Println() // 使用已知的 HMAC 密钥和密码创建新的哈希值和盐值 fmt.Println("Creating new hash and salt values...") h, s, err := New(hmac, []byte(pass)) if err != nil { fmt.Printf("%sn", err) } // 验证新值,失败! fmt.Println("Checking new hash and salt values...") chk, err = Check(hmac, h, []byte(pass), s) if err != nil { fmt.Printf("%sn", err) } fmt.Printf("%tn", chk)}
运行以上代码,会发现使用已知值验证密码时成功,但使用新生成的哈希值和盐值验证密码时失败。这是因为 New() 函数中调用 hash() 函数时,参数顺序错误。
解决方案
Check() 函数中 hash() 函数的调用方式是正确的:
Shakker
多功能AI图像生成和编辑平台
103 查看详情
hchk, err := hash(hmk, pw, s)
而在 New() 函数中,hash() 函数的调用方式是错误的:
h, err = hash(pw, hmk, s)
正确的调用方式应该是:
h, err = hash(hmk, pw, s)
修改后的 New() 函数如下:
// New 函数:生成新的盐值和哈希值func New(hmk, pw []byte) (h, s []byte, err error) { s = make([]byte, KEYLENGTH) _, err = io.ReadFull(rand.Reader, s) if err != nil { return nil, nil, err } h, err = hash(hmk, pw, s) // 修改此处 if err != nil { return nil, nil, err } fmt.Printf("Hash: %xnSalt: %xnPass: %xn", h, s, []byte(pw)) return h, s, nil}
总结与注意事项
仔细检查函数参数顺序: 在调用参数类型相同的函数时,务必仔细检查参数顺序,避免出现类似错误。使用类型系统: 可以考虑使用更严格的类型系统,例如定义结构体来表示 HMAC 密钥、密码和盐值,以避免参数顺序错误。编写单元测试: 编写充分的单元测试是发现此类错误的有效方法。在修改代码后,务必运行单元测试,确保代码的正确性。代码版本控制: 使用 Git 等版本控制工具,可以方便地回溯代码,查找错误原因。HMAC Key 的安全性: HMAC Key 必须保密,否则攻击者可以伪造哈希值,绕过密码验证。
通过以上步骤,可以有效地排查和解决密码认证库中 crypto 包多次调用返回不同结果的问题,确保密码认证的安全性。在实际开发中,应重视代码质量,编写清晰、易懂的代码,并进行充分的测试,以避免出现类似错误。
以上就是Go 密码认证库问题排查:crypto 多次调用返回不同结果的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1133261.html
微信扫一扫
支付宝扫一扫