From f92a529502ea870158fd7913dca3193f2ed7455c Mon Sep 17 00:00:00 2001 From: Nikita Tokarchuk Date: Thu, 9 Jan 2020 12:25:15 +0100 Subject: [PATCH] improve first answer validation --- task1-backend/lib/api.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/task1-backend/lib/api.go b/task1-backend/lib/api.go index d880748..5d3c803 100644 --- a/task1-backend/lib/api.go +++ b/task1-backend/lib/api.go @@ -8,6 +8,7 @@ import ( "encoding/json" "fmt" "net/http" + "regexp" "strings" "time" @@ -35,8 +36,8 @@ type Api struct { } type Answer struct { - Answer json.RawMessage `json:"answer"` - Name string `json:"name"` + Answer string `json:"answer"` + Name string `json:"name"` } func (a *Api) GetHandler() fasthttp.RequestHandler { @@ -85,8 +86,12 @@ func (a *Api) checkAnswer(ctx *routing.Context) (err error) { return routing.NewHTTPError(http.StatusBadRequest, err.Error()) } + cleaner := regexp.MustCompile("[[:^alnum:]]") + pure := cleaner.ReplaceAllLiteralString(answer.Answer, "") + pure = strings.ToLower(pure) + hash := sha256.New() - hash.Write(answer.Answer) + hash.Write([]byte(pure)) hexed := make([]byte, hex.EncodedLen(sha256.Size))