mirror of
https://github.com/hay-kot/homebox.git
synced 2024-11-24 17:45:41 +00:00
03df23d97c
* fix inaccruate 401 error on SQL db error
* init golangci-lint config
* linter autofix
* testify auto fixes
* fix sqlite busy errors
* fix naming
* more linter errors
* fix rest of linter issues
Former-commit-id: e8449b3a73
37 lines
724 B
Go
37 lines
724 B
Go
package hasher
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
var enabled = true
|
|
|
|
func init() { // nolint: gochecknoinits
|
|
disableHas := os.Getenv("UNSAFE_DISABLE_PASSWORD_PROJECTION") == "yes_i_am_sure"
|
|
|
|
if disableHas {
|
|
fmt.Println("WARNING: Password protection is disabled. This is unsafe in production.")
|
|
enabled = false
|
|
}
|
|
}
|
|
|
|
func HashPassword(password string) (string, error) {
|
|
if !enabled {
|
|
return password, nil
|
|
}
|
|
|
|
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
|
|
return string(bytes), err
|
|
}
|
|
|
|
func CheckPasswordHash(password, hash string) bool {
|
|
if !enabled {
|
|
return password == hash
|
|
}
|
|
|
|
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
|
return err == nil
|
|
}
|