亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何驗證密碼

如何驗證密碼

Go
郎朗坤 2022-09-19 10:02:09
在此代碼中,第一個函數是將查找數據庫中的電子郵件地址和以哈希形式存在的密碼。因此,比較哈希和密碼。Findaccount()CompareHashAndPassword()現在在文件中,我有一個名為的函數,它將允許用戶登錄。我在這里有一個問題。我調用了函數,但它只是驗證一個電子郵件地址,沒有驗證正確的密碼,并給我消息。handler.gologinData()database.Findaccount(email, password, hash)false但是,如果我像這樣調用該函數,它會驗證電子郵件和密碼。database.Findaccount(email, "1234", hash)如何解決這個問題,因為我將無法記住每個密碼。數據庫func Findaccount(myEmail, myPassword, hash string) bool {    collection := Connect.Database("WebApp2").Collection("dataStored")    if err := collection.FindOne(context.TODO(), bson.M{"email": myEmail}).Decode(&Account); err != nil {        fmt.Println("Enter the correct email or password")    }    err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(myPassword))    return err == nil}處理程序.gofunc HashPassword(password string) (string, error) {    bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)    return string(bytes), err}func loginData(w http.ResponseWriter, r *http.Request) {    email := r.FormValue("email")    password := r.FormValue("password")    hash, _ := HashPassword(password)    match := database.Findaccount(email, password, hash) // here is a problem    if match == false {        fmt.Println("false")    } else {        fmt.Println("true")    }}
查看完整描述

1 回答

?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

根據文檔,這是 的 func 模式:bycrypt.CompareHashAndPassword()


func CompareHashAndPassword(hashedPassword, password []byte) error

要使用它,您需要將(存儲在數據庫中的哈希密碼)作為第一個參數值。hashedPassword


然后將 from 請求參數放入第二個參數。password


func loginData(w http.ResponseWriter, r *http.Request) {

    email := r.FormValue("email")

    password := r.FormValue("password")

    match := database.Findaccount(email, password)

    if match == false {

        fmt.Println("false")

    } else {

        fmt.Println("true")

    }

}


func Findaccount(myEmail, myPassword string) bool {

    collection := Connect.Database("WebApp2").Collection("dataStored")

    if err := collection.FindOne(context.TODO(), bson.M{"email": myEmail}).Decode(&Account); err != nil {

        fmt.Println("Enter the correct email or password")

    }

    err := bcrypt.CompareHashAndPassword([]byte(Account.Password), []byte(myPassword))

    return err == nil

}

參見 ,語句的第一個參數被填充,它是存儲在 db 上的哈希密碼。Findaccount()bcrypt.CompareHashAndPassword()Account.Password


查看完整回答
反對 回復 2022-09-19
  • 1 回答
  • 0 關注
  • 120 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號