Skip to content

Commit

Permalink
decodeBase64, encodeBase64 and sha256 for yagpdb custom commands (bot…
Browse files Browse the repository at this point in the history
…labs-gg#1679)

* decodeBase64, encodeBase64 and sha256

* decodeBase64, encodeBase64 and sha256
  • Loading branch information
Borbot33 authored and ashishjh-bst committed Jun 27, 2024
1 parent c0fc03d commit d05a33a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions common/templates/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,10 @@ 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: 23 additions & 0 deletions common/templates/context_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ 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 @@ -2422,3 +2424,24 @@ 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
}

0 comments on commit d05a33a

Please sign in to comment.