1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 10:18:38 +00:00

Cleanup models.User.HashPassword (#3334)

This commit is contained in:
Kim "BKC" Carlbäcker
2018-01-11 23:19:38 +01:00
committed by Lauris BH
parent 9aed18073d
commit e5b8b4b5ec
7 changed files with 18 additions and 23 deletions

View File

@@ -388,17 +388,20 @@ func (u *User) NewGitSig() *git.Signature {
}
}
func hashPassword(passwd, salt string) string {
tempPasswd := pbkdf2.Key([]byte(passwd), []byte(salt), 10000, 50, sha256.New)
return fmt.Sprintf("%x", tempPasswd)
}
// HashPassword hashes a password using PBKDF.
func (u *User) HashPassword() {
newPasswd := pbkdf2.Key([]byte(u.Passwd), []byte(u.Salt), 10000, 50, sha256.New)
u.Passwd = fmt.Sprintf("%x", newPasswd)
func (u *User) HashPassword(passwd string) {
u.Passwd = hashPassword(passwd, u.Salt)
}
// ValidatePassword checks if given password matches the one belongs to the user.
func (u *User) ValidatePassword(passwd string) bool {
newUser := &User{Passwd: passwd, Salt: u.Salt}
newUser.HashPassword()
return subtle.ConstantTimeCompare([]byte(u.Passwd), []byte(newUser.Passwd)) == 1
tempHash := hashPassword(passwd, u.Salt)
return subtle.ConstantTimeCompare([]byte(u.Passwd), []byte(tempHash)) == 1
}
// IsPasswordSet checks if the password is set or left empty
@@ -711,7 +714,7 @@ func CreateUser(u *User) (err error) {
if u.Salt, err = GetUserSalt(); err != nil {
return err
}
u.HashPassword()
u.HashPassword(u.Passwd)
u.AllowCreateOrganization = setting.Service.DefaultAllowCreateOrganization
u.MaxRepoCreation = -1

View File

@@ -135,13 +135,11 @@ func TestHashPasswordDeterministic(t *testing.T) {
pass := string(b)
// save the current password in the user - hash it and store the result
u.Passwd = pass
u.HashPassword()
u.HashPassword(pass)
r1 := u.Passwd
// run again
u.Passwd = pass
u.HashPassword()
u.HashPassword(pass)
r2 := u.Passwd
// assert equal (given the same salt+pass, the same result is produced)
@@ -158,7 +156,6 @@ func BenchmarkHashPassword(b *testing.B) {
u := &User{Salt: string(bs), Passwd: pass}
b.ResetTimer()
for i := 0; i < b.N; i++ {
u.HashPassword()
u.Passwd = pass
u.HashPassword(pass)
}
}