mirror of
https://github.com/filebrowser/filebrowser.git
synced 2025-06-30 13:12:57 +00:00
48 lines
1.2 KiB
Go
48 lines
1.2 KiB
Go
package users
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/base64"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
fbErrors "github.com/filebrowser/filebrowser/v2/errors"
|
|
)
|
|
|
|
// ValidateAndHashPwd validates and hashes a password.
|
|
func ValidateAndHashPwd(password string, minimumLength uint) (string, error) {
|
|
if uint(len(password)) < minimumLength {
|
|
return "", fbErrors.ErrShortPassword{MinimumLength: minimumLength}
|
|
}
|
|
|
|
if _, ok := commonPasswords[password]; ok {
|
|
return "", fbErrors.ErrEasyPassword
|
|
}
|
|
|
|
return HashPwd(password)
|
|
}
|
|
|
|
// HashPwd hashes a password.
|
|
func HashPwd(password string) (string, error) {
|
|
bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
return string(bytes), err
|
|
}
|
|
|
|
// CheckPwd checks if a password is correct.
|
|
func CheckPwd(password, hash string) bool {
|
|
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
|
return err == nil
|
|
}
|
|
|
|
func RandomPwd(passwordLength uint) (string, error) {
|
|
randomPasswordBytes := make([]byte, passwordLength)
|
|
var _, err = rand.Read(randomPasswordBytes)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
// This is done purely to make the password human-readable
|
|
var randomPasswordString = base64.URLEncoding.EncodeToString(randomPasswordBytes)
|
|
return randomPasswordString, nil
|
|
}
|