如何使用Go语言验证明文密码是否与盐值MD5密码相同?

huangapple go评论109阅读模式
英文:

How to use go to verify whether the plaintext password is the same as the salt md5 password?

问题

你想用Go语言实现ldap_salted_md5.verify这一步骤。以下是翻译的内容:

Python:

  1. // 生成密码
  2. ldap_salted_md5.hash("123456")
  3. // 验证密码
  4. ldap_salted_md5.verify("123456","{SMD5}991RjK3DQCT+ri/yxQB613Yuxdg=")
  5. // 返回true

Shell:

  1. // 生成密码
  2. slappasswd -h {SMD5} -s "123456"
  3. // 返回 {SMD5}ZmDHoIiZZG/weuCNkLj189sFoPM=
  4. // 通过Python验证密码
  5. ldap_salted_md5.verify("123456","{SMD5}ZmDHoIiZZG/weuCNkLj189sFoPM=")
  6. // 返回True

我明白你想要用Go语言实现ldap_salted_md5.verify这一步骤。

英文:

Python:

  1. # generate password
  2. ldap_salted_md5.hash("123456")
  3. # verify password
  4. ldap_salted_md5.verify("123456","{SMD5}991RjK3DQCT+ri/yxQB613Yuxdg=")
  5. # return true

shell:

  1. # generate password
  2. slappasswd -h {SMD5} -s "123456"
  3. # return {SMD5}ZmDHoIiZZG/weuCNkLj189sFoPM=
  4. # verify password by python
  5. ldap_salted_md5.verify("123456","{SMD5}ZmDHoIiZZG/weuCNkLj189sFoPM=")
  6. # return True

I want to use go to implement this step of ldap_salted_md5.verify

答案1

得分: 0

  1. saltPassword := strings.Replace(user.Password, "{SMD5}", "", -1)
  2. decodeSaltPassword, _ := base64.StdEncoding.DecodeString(saltPassword)
  3. checksum := decodeSaltPassword[0:16]
  4. salt := decodeSaltPassword[16:]
  5. md5Ctx := md5.New()
  6. md5Ctx.Write([]byte(bindSimplePw))
  7. md5Ctx.Write(salt)
  8. cipherStr := md5Ctx.Sum(nil)
  9. if !bytes.Equal(checksum, cipherStr) {
  10. return ldap.LDAPResultInvalidCredentials, nil
  11. }

以上代码是一个Go语言的代码片段,它执行了一些密码验证的操作。具体的步骤如下:

  1. 将用户的密码中的"{SMD5}"替换为空字符串,得到saltPassword。
  2. 使用base64解码saltPassword,得到decodeSaltPassword。
  3. 从decodeSaltPassword中取出前16个字节作为checksum,后面的字节作为salt。
  4. 创建一个md5的上下文对象md5Ctx。
  5. 将bindSimplePw转换为字节数组后写入md5Ctx。
  6. 将salt写入md5Ctx。
  7. 计算md5的哈希值,得到cipherStr。
  8. 如果checksum和cipherStr不相等,则返回ldap.LDAPResultInvalidCredentials,否则继续执行其他操作。

这段代码的作用是验证用户输入的密码是否正确。

英文:
  1. saltPassword := strings.Replace(user.Password, "{SMD5}", "", -1)
  2. decodeSaltPassword, _ := base64.StdEncoding.DecodeString(saltPassword)
  3. checksum := decodeSaltPassword[0:16]
  4. salt := decodeSaltPassword[16:]
  5. md5Ctx := md5.New()
  6. md5Ctx.Write([]byte(bindSimplePw))
  7. md5Ctx.Write(salt)
  8. cipherStr := md5Ctx.Sum(nil)
  9. if !bytes.Equal(checksum, cipherStr) {
  10. return ldap.LDAPResultInvalidCredentials, nil
  11. }

huangapple
  • 本文由 发表于 2022年10月28日 17:34:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/74233350.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定