duandui2803 2018-04-27 11:13
浏览 97
已采纳

相同的字符串导致不同的MD5哈希

Its pretty straightforward. The passes don't match. I can't find why.

The hashing function

package utils
var hasher = md5.New()

func GetMD5Hash(text string) string {

    fmt.Println(">> ", text, "<<")
    hasher.Write([]byte(text))
    return hex.EncodeToString(hasher.Sum(nil))
}
`  

I make 1st restful call to create a user (register) calls this method.

func CreateUser(id bson.ObjectId, email string, password string) bool {
    var user User
    user.ID = id
    user.Email = email
    user.Password = utils.GetMD5Hash(password)

    // fmt.Println("In Create User:", password, user.Password)

    err := userCollection.Insert(&user)

    if err != nil {
        log.Println("Error while CreateUser")
        return false
    }
    return true
}

Now I make 2nd restful call to authenticate (login)

The Authenticate User Method

func AuthenticateUser(email string, password string) bool {
    user := GetUserByEmail(email)

    var u User

    u.Password = utils.GetMD5Hash(password)
    fmt.Println("In Authenticate:", password, u.Password)
    fmt.Println(u.Password, user.Password)

    if u.Password == user.Password {
        return true
    }
    return false
}

PASSWORDS DONT MATCH.

Results:

>>  pass <<
In Authenticate: pass 1a1dc91c907325c69271ddf0c944bc72
1a1dc91c907325c69271ddf0c944bc72 1a1dc91c907325c69271ddf0c944bc72
>>  pass <<
{ObjectIdHex("5ae3746e1b2a612417149bca") []}
>>  pass <<
In Authenticate: pass dbe4a8e3a3b93ed3101bace4bc19fc70
dbe4a8e3a3b93ed3101bace4bc19fc70 078bbb4bf0f7117fb131ec45f15b5b87
>>  pass <<
{ObjectIdHex("5ae374a31b2a612417149bcb") []}
>>  pass <<
In Authenticate: pass c4e6ffe7c63bb65e68521293416c96a2
c4e6ffe7c63bb65e68521293416c96a2 1bdfd5f0b03c0d80557384602303c690
>>  pass <<
In Authenticate: pass c6d90629ad5c6b8edbe479340d5bed6b
c6d90629ad5c6b8edbe479340d5bed6b 1bdfd5f0b03c0d80557384602303c690
>>  pass <<
In Authenticate: pass 5b54f4793a13c985b4f4275980542496
5b54f4793a13c985b4f4275980542496 1bdfd5f0b03c0d80557384602303c690

If it is not clear, I am literally making the same request repeatedly and every time results in a new MD5Hash. Also, both the register and login process uses the same UI, same text boxes different buttons.

Insights. Please.

展开全部

  • 写回答

1条回答 默认 最新

  • duan201444 2018-04-27 11:20
    关注

    It appears as though you are writing over and over to the same (package level variable) hasher, so that at first it contains 'pass', then 'passpass', then 'passpasspass' (as []byte of course) so the result changes because the underlying bytes are changing. Try putting in the var hasher = md5.New() inside the GetMD5Hash function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部