From 09517318b87e39506cbcb3232a395ac7d43c25f5 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Tue, 23 Sep 2025 21:18:49 -0700 Subject: Use pbkdf2 for password hashing instead of sha512 --- roseh.moe.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'roseh.moe.go') diff --git a/roseh.moe.go b/roseh.moe.go index b8e6e21..a15fd9b 100644 --- a/roseh.moe.go +++ b/roseh.moe.go @@ -4,7 +4,6 @@ import ( "bytes" "crypto/ed25519" "crypto/rand" - "crypto/sha512" "crypto/subtle" "embed" "encoding/base64" @@ -19,6 +18,8 @@ import ( "path/filepath" "strings" "time" + + "gitlab.com/rhogenson/roseh.moe/internal/pwhash" ) var ( @@ -30,9 +31,10 @@ var ( ) var ( - notepadPassword []byte - privateKey ed25519.PrivateKey - publicKey ed25519.PublicKey + notepadPassword []byte + notepadPasswordSalt []byte + privateKey ed25519.PrivateKey + publicKey ed25519.PublicKey ) func loadSecrets() error { @@ -56,6 +58,11 @@ func loadSecrets() error { } privateKey = ed25519.NewKeyFromSeed(seed) publicKey = privateKey.Public().(ed25519.PublicKey) + } else if salt, ok := bytes.CutPrefix(line, []byte("salt=")); ok { + notepadPasswordSalt = make([]byte, hex.DecodedLen(len(salt))) + if _, err := hex.Decode(notepadPasswordSalt, salt); err != nil { + return err + } } } return nil @@ -204,7 +211,11 @@ type loginTemplateArgs struct { } func login(w http.ResponseWriter, r *http.Request) { - pwHash := sha512.Sum512([]byte(r.FormValue("password"))) + pwHash, err := pwhash.Hash(r.FormValue("password"), notepadPasswordSalt) + if err != nil { + http.Error(w, fmt.Sprintf("Unable to hash password: %s", err), http.StatusInternalServerError) + return + } if subtle.ConstantTimeCompare(pwHash[:], notepadPassword) == 0 { if err := loginTemplate.Execute(w, loginTemplateArgs{Error: true, Redirect: r.FormValue("redirect")}); err != nil { log.Printf("Warning: login: %s", err) -- cgit v1.3.1