aboutsummaryrefslogtreecommitdiff
path: root/internal/player/password.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/player/password.go')
-rw-r--r--internal/player/password.go32
1 files changed, 32 insertions, 0 deletions
diff --git a/internal/player/password.go b/internal/player/password.go
new file mode 100644
index 0000000..17e4218
--- /dev/null
+++ b/internal/player/password.go
@@ -0,0 +1,32 @@
+package player
+
+import (
+ "crypto/rand"
+ "crypto/sha256"
+ "encoding/base64"
+ "fmt"
+ "strings"
+)
+
+func HashPassword(password string) (string, error) {
+ salt := make([]byte, 16)
+ if _, err := rand.Read(salt); err != nil {
+ return "", err
+ }
+ hash := sha256.Sum256(append(salt, []byte(password)...))
+ return fmt.Sprintf("sha256:%s:%x", base64.RawStdEncoding.EncodeToString(salt), hash), nil
+}
+
+func CheckPassword(password, stored string) bool {
+ parts := strings.SplitN(stored, ":", 3)
+ if len(parts) != 3 || parts[0] != "sha256" {
+ return false
+ }
+ salt, err := base64.RawStdEncoding.DecodeString(parts[1])
+ if err != nil {
+ return false
+ }
+ hash := sha256.Sum256(append(salt, []byte(password)...))
+ expected := fmt.Sprintf("%x", hash)
+ return parts[2] == expected
+}