2022-08-30 02:30:36 +00:00
|
|
|
package hasher
|
|
|
|
|
2022-10-17 02:50:44 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
)
|
|
|
|
|
|
|
|
var enabled = true
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
disableHas := os.Getenv("UNSAFE_DISABLE_PASSWORD_PROJECTION") == "yes_i_am_sure"
|
|
|
|
|
|
|
|
if disableHas {
|
2022-12-10 05:57:57 +00:00
|
|
|
fmt.Println("WARNING: Password protection is disabled. This is unsafe in production.")
|
2022-10-17 02:50:44 +00:00
|
|
|
enabled = false
|
|
|
|
}
|
|
|
|
}
|
2022-08-30 02:30:36 +00:00
|
|
|
|
|
|
|
func HashPassword(password string) (string, error) {
|
2022-10-17 02:50:44 +00:00
|
|
|
if !enabled {
|
|
|
|
return password, nil
|
|
|
|
}
|
|
|
|
|
2022-08-30 02:30:36 +00:00
|
|
|
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
|
|
|
|
return string(bytes), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func CheckPasswordHash(password, hash string) bool {
|
2022-10-17 02:50:44 +00:00
|
|
|
if !enabled {
|
|
|
|
return password == hash
|
|
|
|
}
|
|
|
|
|
2022-08-30 02:30:36 +00:00
|
|
|
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
|
|
|
return err == nil
|
|
|
|
}
|