Go - two bcrypt hashes of the same password are NOT equal

发布时间 2023-12-05 21:02:09作者: ZhangZhihuiAAA

 

package main

import (
    "fmt"

    "golang.org/x/crypto/bcrypt"
)

func main() {
    password := "abcdef"
    hashedPassword1, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
    fmt.Println(string(hashedPassword1))
    hashedPassword2, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
    fmt.Println(string(hashedPassword2))

    if string(hashedPassword1) == string(hashedPassword2) {
        fmt.Println("two hashes are equal")
    } else {
        fmt.Println("two hashes of the same password are NOT equal")
    }

    if bcrypt.CompareHashAndPassword(hashedPassword1, []byte(password)) == nil {
        fmt.Println("correct password")
    }
    if bcrypt.CompareHashAndPassword(hashedPassword2, []byte(password)) == nil {
        fmt.Println("correct password")
    }
}

 

zzh@ZZHPC:/zdata/Github/zimplebank$ go run aaa.go
$2a$10$Vnp9Xxtx0xvCh2yM7BySTu7V4jB8UefMDoqNtsB5XE2/35STQLc/O
$2a$10$/FanKJVVNsjglMVxXyslw.UQh2y6KFWzqAY3qM.6i/Eli7FAKir02
two hashes of the same password are NOT equal
correct password
correct password