Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions expr/builtins/hash_and_encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,22 @@ func encodeB64DecodeEval(ctx expr.EvalContext, args []value.Value) (value.Value,
}
return value.NewStringValue(string(by)), true
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im going to talk this one over with the team tomorrow. If the goal here is to have this generate a random salt, that is only set once and never changed. We may need to write this as one of our internal built-ins, so it has context from the entire entity. Otherwise each time the profile is evaluated it's salt will change, which will change it's hashed email address.

// RandomSalt generates a random ten-digit number as a string
//
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to register your function as well here:

expr.FuncAdd("hash.sha512", &HashSha512{})

something like:

		expr.FuncAdd("random.salt", &RandomSalt{})

// random_salt() => "1234567890"
type RandomSalt struct{}

// Type string
func (m *RandomSalt) Type() value.ValueType { return value.StringType }
func (m *RandomSalt) Validate(n *expr.FuncNode) (expr.EvaluatorFunc, error) {
if len(n.Args) != 0 {
return nil, fmt.Errorf("Expected 0 args for random_salt() but got %s", n)
}
return randomSaltEval, nil
}
func randomSaltEval(ctx expr.EvalContext, args []value.Value) (value.Value, bool) {
// Generate a random number between 1000000000 and 9999999999
randomNum := rand.Int63n(9000000000) + 1000000000
Copy link
Contributor

@epsniff epsniff May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to use the crypto rand here? I think we need to fix the imports for the file too...

import "crypto/rand"
...
	salt := make([]byte, 10)
	if ib, err := rand.Read(salt); err != nil && ib != len(salt) {
		return value.EmptyStringValue, false
	}

	return value.NewStringValue(base64.URLEncoding.EncodeToString(salt)), true

return value.NewStringValue(fmt.Sprintf("%d", randomNum)), true
}