mirror of
https://github.com/wneessen/logranger.git
synced 2024-11-22 12:50:50 +01:00
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.
This commit is contained in:
parent
b6f6b6a664
commit
68b1544125
1 changed files with 68 additions and 1 deletions
|
@ -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))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue