File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ import (
2323// Funcs is the mapping of built-in functions for configuration.
2424func Funcs () map [string ]ast.Function {
2525 return map [string ]ast.Function {
26+ "base64sha256" : interpolationFuncBase64Sha256 (),
2627 "cidrhost" : interpolationFuncCidrHost (),
2728 "cidrnetmask" : interpolationFuncCidrNetmask (),
2829 "cidrsubnet" : interpolationFuncCidrSubnet (),
@@ -605,6 +606,7 @@ func interpolationFuncSha1() ast.Function {
605606 }
606607}
607608
609+ // hexadecimal representation of sha256 sum
608610func interpolationFuncSha256 () ast.Function {
609611 return ast.Function {
610612 ArgTypes : []ast.Type {ast .TypeString },
@@ -629,3 +631,18 @@ func interpolationFuncTrimSpace() ast.Function {
629631 },
630632 }
631633}
634+
635+ func interpolationFuncBase64Sha256 () ast.Function {
636+ return ast.Function {
637+ ArgTypes : []ast.Type {ast .TypeString },
638+ ReturnType : ast .TypeString ,
639+ Callback : func (args []interface {}) (interface {}, error ) {
640+ s := args [0 ].(string )
641+ h := sha256 .New ()
642+ h .Write ([]byte (s ))
643+ shaSum := h .Sum (nil )
644+ encoded := base64 .StdEncoding .EncodeToString (shaSum [:])
645+ return encoded , nil
646+ },
647+ }
648+ }
Original file line number Diff line number Diff line change @@ -849,7 +849,7 @@ func TestInterpolateFuncSha1(t *testing.T) {
849849func TestInterpolateFuncSha256 (t * testing.T ) {
850850 testFunction (t , testFunctionConfig {
851851 Cases : []testFunctionCase {
852- {
852+ { // hexadecimal representation of sha256 sum
853853 `${sha256("test")}` ,
854854 "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08" ,
855855 false ,
@@ -870,6 +870,23 @@ func TestInterpolateFuncTrimSpace(t *testing.T) {
870870 })
871871}
872872
873+ func TestInterpolateFuncBase64Sha256 (t * testing.T ) {
874+ testFunction (t , testFunctionConfig {
875+ Cases : []testFunctionCase {
876+ {
877+ `${base64sha256("test")}` ,
878+ "n4bQgYhMfWWaL+qgxVrQFaO/TxsrC4Is0V1sFbDwCgg=" ,
879+ false ,
880+ },
881+ { // This will differ because we're base64-encoding hex represantiation, not raw bytes
882+ `${base64encode(sha256("test"))}` ,
883+ "OWY4NmQwODE4ODRjN2Q2NTlhMmZlYWEwYzU1YWQwMTVhM2JmNGYxYjJiMGI4MjJjZDE1ZDZjMTViMGYwMGEwOA==" ,
884+ false ,
885+ },
886+ },
887+ })
888+ }
889+
873890type testFunctionConfig struct {
874891 Cases []testFunctionCase
875892 Vars map [string ]ast.Variable
You can’t perform that action at this time.
0 commit comments