
第一段引用上面的摘要:
本文旨在帮助开发者排查和解决 Go 语言密码认证库中 crypto 包多次调用返回不同结果的问题。通过分析问题代码,找出 hash 函数参数顺序错误,并提供修改建议,确保密码认证的正确性。本文适合对 Go 语言和密码学有一定了解的开发者阅读。
在开发密码认证库时,经常会遇到多次调用加密函数,但结果不一致的问题。这会导致验证失败,影响系统的安全性。本文将以一个实际案例为例,分析问题原因,并提供解决方案。
以下代码展示了一个密码认证库的实现,包含 Check() 和 New() 两个函数,分别用于验证密码和生成新的盐值及哈希值。
package main
import (
"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: %x\nHMAC: %x\nSalt: %x\nPass: %x\n", h, hmk, s, []byte(pw))
hchk, err := hash(hmk, pw, s)
if err != nil {
return false, err
}
fmt.Printf("Hchk: %x\n", 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: %x\nSalt: %x\nPass: %x\n", 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("%s\n", err)
}
fmt.Printf("%t\n", chk)
fmt.Println()
// 使用已知的 HMAC 密钥和密码创建新的哈希值和盐值
fmt.Println("Creating new hash and salt values...")
h, s, err := New(hmac, []byte(pass))
if err != nil {
fmt.Printf("%s\n", err)
}
// 验证新值,失败!
fmt.Println("Checking new hash and salt values...")
chk, err = Check(hmac, h, []byte(pass), s)
if err != nil {
fmt.Printf("%s\n", err)
}
fmt.Printf("%t\n", chk)
}运行以上代码,会发现使用已知值验证密码时成功,但使用新生成的哈希值和盐值验证密码时失败。这是因为 New() 函数中调用 hash() 函数时,参数顺序错误。
Check() 函数中 hash() 函数的调用方式是正确的:
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: %x\nSalt: %x\nPass: %x\n", h, s, []byte(pw))
return h, s, nil
}通过以上步骤,可以有效地排查和解决密码认证库中 crypto 包多次调用返回不同结果的问题,确保密码认证的安全性。在实际开发中,应重视代码质量,编写清晰、易懂的代码,并进行充分的测试,以避免出现类似错误。
以上就是Go 密码认证库问题排查:crypto 多次调用返回不同结果的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号