From 81b975d85412567bd7e88e341d391dcf96204b08 Mon Sep 17 00:00:00 2001 From: Ilyes512 Date: Fri, 27 Jul 2018 22:11:30 +0200 Subject: [PATCH] Added cryptographically secure pseudorandom base64 string --- pkg/template/functions.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pkg/template/functions.go b/pkg/template/functions.go index 78c5f0f..2f95924 100644 --- a/pkg/template/functions.go +++ b/pkg/template/functions.go @@ -1,6 +1,8 @@ package template import ( + "crypto/rand" + "encoding/base64" "fmt" "os" "os/user" @@ -95,6 +97,18 @@ var ( return res }, + + // generate a random base64 string based on random bytes of length n + "randomBase64": func(length int) string { + b := make([]byte, length) + _, err := rand.Read(b) + + if err != nil { + return fmt.Sprintf("failed to generate randomBase64: %s", err) + } + + return base64.StdEncoding.EncodeToString(b) + }, } // Options contain the default options for the template execution.