cmd/anubis: do not return error from sha256 (#57)

hash.Write never returns error so removing it from
the results simplifies usage and eliminates dead error handling.

Signed-off-by: Alexander Yastrebov <yastrebov.alex@gmail.com>
This commit is contained in:
Alexander Yastrebov 2025-03-21 20:46:43 +01:00 committed by GitHub
parent 4ec4dc3624
commit 194e55088b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 7 additions and 23 deletions

View file

@ -266,13 +266,10 @@ func metricsServer(ctx context.Context, done func()) {
}
}
func sha256sum(text string) (string, error) {
func sha256sum(text string) string {
hash := sha256.New()
_, err := hash.Write([]byte(text))
if err != nil {
return "", err
}
return hex.EncodeToString(hash.Sum(nil)), nil
hash.Write([]byte(text))
return hex.EncodeToString(hash.Sum(nil))
}
func (s *Server) challengeFor(r *http.Request, difficulty int) string {
@ -287,8 +284,7 @@ func (s *Server) challengeFor(r *http.Request, difficulty int) string {
fp,
difficulty,
)
result, _ := sha256sum(data)
return result
return sha256sum(data)
}
func New(target, policyFname string) (*Server, error) {
@ -522,13 +518,7 @@ func (s *Server) maybeReverseProxy(w http.ResponseWriter, r *http.Request) {
}
calcString := fmt.Sprintf("%s%d", challenge, nonce)
calculated, err := sha256sum(calcString)
if err != nil {
lg.Error("failed to calculate sha256sum", "path", r.URL.Path, "err", err)
clearCookie(w)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
calculated := sha256sum(calcString)
if subtle.ConstantTimeCompare([]byte(claims["response"].(string)), []byte(calculated)) != 1 {
lg.Debug("invalid response", "path", r.URL.Path)
@ -635,13 +625,7 @@ func (s *Server) passChallenge(w http.ResponseWriter, r *http.Request) {
}
calcString := fmt.Sprintf("%s%d", challenge, nonce)
calculated, err := sha256sum(calcString)
if err != nil {
clearCookie(w)
lg.Debug("can't parse shasum", "err", err)
templ.Handler(base("Oh noes!", errorPage("failed to calculate sha256sum")), templ.WithStatus(http.StatusInternalServerError)).ServeHTTP(w, r)
return
}
calculated := sha256sum(calcString)
if subtle.ConstantTimeCompare([]byte(response), []byte(calculated)) != 1 {
clearCookie(w)

View file

@ -50,7 +50,7 @@ func (b Bot) Hash() (string, error) {
userAgentRex = b.UserAgent.String()
}
return sha256sum(fmt.Sprintf("%s::%s::%s", b.Name, pathRex, userAgentRex))
return sha256sum(fmt.Sprintf("%s::%s::%s", b.Name, pathRex, userAgentRex)), nil
}
func parseConfig(fin io.Reader, fname string, defaultDifficulty int) (*ParsedConfig, error) {