commit f8dca7b0318727797e13c1a01848cc79da02cdf5
parent 0fb00ba58515959864bf1bef68e0c3d605b57d7a
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Sat, 27 May 2023 21:17:16 -0700
cleanup
Diffstat:
3 files changed, 13 insertions(+), 11 deletions(-)
diff --git a/pkg/web/handlers/chat.go b/pkg/web/handlers/chat.go
@@ -1,17 +1,14 @@
package handlers
import (
- "crypto/sha256"
"dkforest/pkg/captcha"
"dkforest/pkg/config"
"dkforest/pkg/database"
"dkforest/pkg/managers"
"dkforest/pkg/utils"
hutils "dkforest/pkg/web/handlers/utils"
- "encoding/hex"
"github.com/labstack/echo"
"net/http"
- "strings"
"time"
)
@@ -200,10 +197,7 @@ func handleChatPasswordPost(db *database.DkfDB, c echo.Context, data chatData, a
// verify POW
if config.PowEnabled.IsTrue() {
- h := sha256.Sum256([]byte(data.GuestUsername + ":" + data.Pow))
- hashed := hex.EncodeToString(h[:])
- prefix := strings.Repeat("0", config.PowDifficulty)
- if !strings.HasPrefix(hashed, prefix) {
+ if !hutils.VerifyPow(database.Username(data.GuestUsername), data.Pow, config.PowDifficulty) {
data.ErrPow = "invalid proof of work"
return c.Render(http.StatusOK, chatPasswordTmplName, data)
}
diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go
@@ -878,10 +878,7 @@ func signupHandler(c echo.Context) error {
// verify POW
if config.PowEnabled.IsTrue() {
- h := sha256.Sum256([]byte(data.Username + ":" + data.Pow))
- hashed := hex.EncodeToString(h[:])
- prefix := strings.Repeat("0", config.PowDifficulty)
- if !strings.HasPrefix(hashed, prefix) {
+ if !hutils.VerifyPow(database.Username(data.Username), data.Pow, config.PowDifficulty) {
data.ErrPow = "invalid proof of work"
return c.Render(http.StatusOK, "standalone.signup", data)
}
diff --git a/pkg/web/handlers/utils/utils.go b/pkg/web/handlers/utils/utils.go
@@ -1,14 +1,18 @@
package utils
import (
+ "crypto/sha256"
"dkforest/pkg/captcha"
+ "dkforest/pkg/database"
"encoding/base64"
+ "encoding/hex"
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"strconv"
+ "strings"
"time"
"dkforest/pkg/config"
@@ -174,3 +178,10 @@ func KillCircuit(c echo.Context) {
config.ConnMap.Close(conn)
}
}
+
+func VerifyPow(username database.Username, nonce string, difficulty int) bool {
+ h := sha256.Sum256([]byte(string(username) + ":" + nonce))
+ hashed := hex.EncodeToString(h[:])
+ prefix := strings.Repeat("0", difficulty)
+ return strings.HasPrefix(hashed, prefix)
+}