ZhangZhihui's Blog  

 

复制代码
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

 

posted on   ZhangZhihuiAAA  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
 
点击右上角即可分享
微信分享提示