From 68b15441254da00cae7af925f5b209d0bbb49a5a Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Sat, 23 Dec 2023 22:00:23 +0100 Subject: [PATCH] Add SHA hashing and base64 encoding methods to template Implemented new template functions to allow conversion of strings to SHA-1, SHA-256, and SHA-512 hash formats, as well as base64 encoding. These changes include the creation of the SHAAlgo type and associated constants representing different SHA algorithms. The additional functionality will enhance template handling by adding more options for string encryption and encoding. --- template/template.go | 69 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/template/template.go b/template/template.go index ec6cecd..c0142d2 100644 --- a/template/template.go +++ b/template/template.go @@ -5,7 +5,13 @@ package template import ( + "crypto/sha1" + "crypto/sha256" + "crypto/sha512" + "encoding/base64" "fmt" + "hash" + "io" "strings" "text/template" "time" @@ -13,6 +19,18 @@ import ( "github.com/wneessen/go-parsesyslog" ) +// SHAAlgo is a enum-like type wrapper representing a SHA algorithm +type SHAAlgo uint + +const ( + // SHA1 is a constant of type SHAAlgo, representing the SHA-1 algorithm. + SHA1 SHAAlgo = iota + // SHA256 is a constant of type SHAAlgo, representing the SHA-256 algorithm. + SHA256 + // SHA512 is a constant of type SHAAlgo, representing the SHA-512 algorithm. + SHA512 +) + // FuncMap represents a mapping of function names to their corresponding // functions. // It is used to define custom functions that can be accessed in Go @@ -60,7 +78,12 @@ func Compile(lm parsesyslog.LogMsg, mg []string, ot string) (string, error) { func NewTemplateFuncMap() template.FuncMap { fm := FuncMap{} return template.FuncMap{ - "_ToLower": fm.ToLower, + "_ToLower": fm.ToLower, + "_ToUpper": fm.ToUpper, + "_ToBase64": fm.ToBase64, + "_ToSHA1": fm.ToSHA1, + "_ToSHA256": fm.ToSHA256, + "_ToSHA512": fm.ToSHA512, } } @@ -73,3 +96,47 @@ func (*FuncMap) ToLower(s string) string { func (*FuncMap) ToUpper(s string) string { return strings.ToUpper(s) } + +// ToBase64 returns the base64 encoding of a given string. +func (*FuncMap) ToBase64(s string) string { + return base64.RawStdEncoding.EncodeToString([]byte(s)) +} + +// ToSHA1 returns the SHA-1 hash of the given string +func (*FuncMap) ToSHA1(s string) string { + return toSHA(s, SHA1) +} + +// ToSHA256 returns the SHA-256 hash of the given string +func (*FuncMap) ToSHA256(s string) string { + return toSHA(s, SHA256) +} + +// ToSHA512 returns the SHA-512 hash of the given string +func (*FuncMap) ToSHA512(s string) string { + return toSHA(s, SHA512) +} + +// toSHA is a function that converts a string to a SHA hash. +// +// The function takes two parameters: a string 's' and a 'sa' of +// type SHAAlgo which defines the SHA algorithm to be used. +func toSHA(s string, sa SHAAlgo) string { + var h hash.Hash + switch sa { + case SHA1: + h = sha1.New() + case SHA256: + h = sha256.New() + case SHA512: + h = sha512.New() + default: + return "" + } + + _, err := io.WriteString(h, s) + if err != nil { + return "" + } + return fmt.Sprintf("%x", h.Sum(nil)) +}