比较Golang中的错误

huangapple go评论94阅读模式
英文:

Comparing errors in golang

问题

我正在使用golang编写一个基本的密码认证系统。
我使用bcrypt来对密码进行哈希处理,并将哈希保存在数据库中。
以下是从数据库中检索经过身份验证的帐户的函数。

func FindAccount(db *gorp.DbMap, email, password string) (*Account, error) {
    account, err := FindByEmail(db, email)
    if err != nil {
        return nil, err
    }
    if account == nil {
        return nil, nil
    }
    if err := bcrypt.CompareHashAndPassword([]byte(account.HashedPassword), []byte(password)); err != nil {
        return nil, err
    }
    return account, nil
}

调用者代码如下:

account, err := FindAccount(db, email, password)
if err != nil {
    if err == bcrypt.ErrMismatchedHashAndPassword {
        log.Printf("为什么这个条件不匹配?")
        return nil, EmailPasswordInvalidError{}
    }

    log.Printf("bcrypt.Err: %p, %#v", bcrypt.ErrMismatchedHashAndPassword, bcrypt.ErrMismatchedHashAndPassword)
    log.Printf("err       : %p, %#v", err, err)
    return nil, err
}

当我使用这段代码并提供无效的电子邮件和密码时,会发生以下情况:

sessions.go:51: bcrypt.Err: 0xc2080290b0, &errors.errorString{s:"crypto/bcrypt: hashedPassword is not the hash of the given password"}
sessions.go:52: err       : 0xc2080291e0, &errors.errorString{s:"crypto/bcrypt: hashedPassword is not the hash of the given password"}

为什么指针地址不同?
我们不能只比较错误吗?

英文:

I'm writing a basic password authentication system in golang.
I use bcrypt to hash the password and save the hash in a database.
Here's the function to retrieve an authenticated account from the database.

func FindAccount(db *gorp.DbMap, email, password string) (*Account, error) {
	account, err := FindByEmail(db, email)
	if err != nil {
		return nil, err
	}
	if account == nil {
		return nil, nil
	}
	if err := bcrypt.CompareHashAndPassword([]byte(account.HashedPassword), []byte(password)); err != nil {
		return nil, err
	}
	return account, nil
}

And the caller:

account, err := FindAccount(db, email, password)
if err != nil {
	if err == bcrypt.ErrMismatchedHashAndPassword {
        log.Printf("Why doesn't this condition match?")
		return nil, EmailPasswordInvalidError{}
	}

	log.Printf("bcrypt.Err: %p, %#v", bcrypt.ErrMismatchedHashAndPassword, bcrypt.ErrMismatchedHashAndPassword)
	log.Printf("err       : %p, %#v", err, err)
	return nil, err
}

And when I use this code and provide invalid email and password, here's what happens:

sessions.go:51: bcrypt.Err: 0xc2080290b0, &errors.errorString{s:"crypto/bcrypt: hashedPassword is not the hash of the given password"}
sessions.go:52: err       : 0xc2080291e0, &errors.errorString{s:"crypto/bcrypt: hashedPassword is not the hash of the given password"}

Why is the pointer address different?
Can't we just compare errors?

答案1

得分: 4

我导入了两个bcrypt包。
导入了"code.google.com/p/go.crypto/bcrypt"的文件中有FindAccount,而调用者导入了"golang.org/x/crypto/bcrypt"

因此,有多个

var ErrMismatchedHashAndPassword = errors.New("crypto/bcrypt: hashedPassword is not the hash of the given password")

具有不同的指针。

将所有的"code.google.com/p/go.crypto/bcrypt"替换为"golang.org/x/crypto/bcrypt"解决了这个问题。

英文:

I had two bcrypt packages imported..
The file which has FindAccount imported "code.google.com/p/go.crypto/bcrypt", and the caller imported "golang.org/x/crypto/bcrypt".

Thus there were multiple

var ErrMismatchedHashAndPassword = errors.New("crypto/bcrypt: hashedPassword is not the hash of the given password")

with different pointers.

Replacing all "code.google.com/p/go.crypto/bcrypt" with "golang.org/x/crypto/bcrypt" fixed the issue.

huangapple
  • 本文由 发表于 2015年6月23日 18:31:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/31000378.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定