duandui2803 2018-04-27 19: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 19: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.

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

报告相同问题?

悬赏问题

  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元