From b87dc007380e4c431c8b8ff2cf3d113f3e5858ac Mon Sep 17 00:00:00 2001 From: Borbot33 <58076174+Borbot33@users.noreply.github.com> Date: Thu, 27 Jun 2024 07:21:52 +0200 Subject: [PATCH] Move the base64 & sha256 functions to general.go and change name of sha256 function to hash (#1682) * moved functions from context_funcs.go to general.go * changed function name from sha256 to hash * moved functions from context_funcs.go to general.go * added a tab --- common/templates/context.go | 7 +++---- common/templates/context_funcs.go | 23 ----------------------- common/templates/general.go | 25 ++++++++++++++++++++++++- 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/common/templates/context.go b/common/templates/context.go index 042a99e3b..3d813db5c 100644 --- a/common/templates/context.go +++ b/common/templates/context.go @@ -113,6 +113,9 @@ var ( "shuffle": shuffle, "verb": common.RandomVerb, + "hash": tmplSha256, + "decodeBase64": tmplDecodeBase64, + "encodeBase64": tmplEncodeBase64, // time functions "currentTime": tmplCurrentTime, @@ -745,10 +748,6 @@ func baseContextFuncs(c *Context) { c.addContextFunc("onlineCountBots", c.tmplOnlineCountBots) c.addContextFunc("sort", c.tmplSort) - - c.addContextFunc("encodeBase64", c.tmplEncodeBase64) - c.addContextFunc("decodeBase64", c.tmplDecodeBase64) - c.addContextFunc("sha256", c.tmplSha256) } type limitedWriter struct { diff --git a/common/templates/context_funcs.go b/common/templates/context_funcs.go index 55c332626..e289fa488 100644 --- a/common/templates/context_funcs.go +++ b/common/templates/context_funcs.go @@ -11,8 +11,6 @@ import ( "strconv" "strings" "time" - "crypto/sha256" - "encoding/base64" "github.com/botlabs-gg/yagpdb/v2/bot" "github.com/botlabs-gg/yagpdb/v2/common" @@ -2424,24 +2422,3 @@ func (c *Context) validateDurationDelay(in interface{}) time.Duration { return ToDuration(t) } } - -func (c *Context) tmplDecodeBase64(str string) (string, error) { - raw, err := base64.StdEncoding.DecodeString(str) - if err != nil { - return "", err - } - return string(raw), nil -} - -func (c *Context) tmplEncodeBase64(str string) string { - return base64.StdEncoding.EncodeToString([]byte(str)) -} - -func (c *Context) tmplSha256(str string) string { - hash := sha256.New() - hash.Write([]byte(str)) - - sha256 := base64.URLEncoding.EncodeToString(hash.Sum(nil)) - - return sha256 -} diff --git a/common/templates/general.go b/common/templates/general.go index c71b9c2f7..7bf6dc6fd 100644 --- a/common/templates/general.go +++ b/common/templates/general.go @@ -10,7 +10,9 @@ import ( "strconv" "strings" "time" - + "crypto/sha256" + "encoding/base64" + "emperror.dev/errors" "github.com/botlabs-gg/yagpdb/v2/bot" "github.com/botlabs-gg/yagpdb/v2/common" @@ -1687,3 +1689,24 @@ func tmplHumanizeDurationSeconds(in interface{}) string { func tmplHumanizeTimeSinceDays(in time.Time) string { return common.HumanizeDuration(common.DurationPrecisionDays, time.Since(in)) } + +func tmplDecodeBase64(str string) (string, error) { + raw, err := base64.StdEncoding.DecodeString(str) + if err != nil { + return "", err + } + return string(raw), nil +} + +func tmplEncodeBase64(str string) string { + return base64.StdEncoding.EncodeToString([]byte(str)) +} + +func tmplSha256(str string) string { + hash := sha256.New() + hash.Write([]byte(str)) + + sha256 := base64.URLEncoding.EncodeToString(hash.Sum(nil)) + + return sha256 +}