mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-06-03 10:59:34 +00:00
The UserPasswordHash seeder bcrypt-hashed user.Password unconditionally, assuming plaintext. If it ran on an already-bcrypt value (DB restore, SQLite<->Postgres switch, history_of_seeders inconsistency on upgrade) it double-hashed the password, locking the admin out with both old and new passwords rejected. Skip any password that is already a bcrypt hash.
23 lines
685 B
Go
23 lines
685 B
Go
// Package crypto provides cryptographic utilities for password hashing and verification.
|
|
package crypto
|
|
|
|
import (
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
// HashPasswordAsBcrypt generates a bcrypt hash of the given password.
|
|
func HashPasswordAsBcrypt(password string) (string, error) {
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
return string(hash), err
|
|
}
|
|
|
|
// CheckPasswordHash verifies if the given password matches the bcrypt hash.
|
|
func CheckPasswordHash(hash, password string) bool {
|
|
return bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) == nil
|
|
}
|
|
|
|
func IsHashed(s string) bool {
|
|
_, err := bcrypt.Cost([]byte(s))
|
|
return err == nil
|
|
}
|