Skip to content

Commit

Permalink
Move the base64 & sha256 functions to general.go and change name of s…
Browse files Browse the repository at this point in the history
…ha256 function to hash (botlabs-gg#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
  • Loading branch information
Borbot33 authored and ashishjh-bst committed Jun 27, 2024
1 parent d5308e9 commit b87dc00
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
7 changes: 3 additions & 4 deletions common/templates/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ var (

"shuffle": shuffle,
"verb": common.RandomVerb,
"hash": tmplSha256,
"decodeBase64": tmplDecodeBase64,
"encodeBase64": tmplEncodeBase64,

// time functions
"currentTime": tmplCurrentTime,
Expand Down Expand Up @@ -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 {
Expand Down
23 changes: 0 additions & 23 deletions common/templates/context_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
25 changes: 24 additions & 1 deletion common/templates/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

0 comments on commit b87dc00

Please sign in to comment.