Hush is a Go package that provides a flexible and efficient way to process and mask sensitive data in structs and strings.
- Process structs and strings etc to mask or hide sensitive information
- Customizable field separators for nested structures
- Support for custom masking functions
- Concurrent processing of struct fields for improved performance
- Option to include or exclude private fields
- Context-aware processing with cancellation support
- Consistent handling of maps and slices
To install Hush, use go get
:
go get github.com/tlmanz/hush
Here's a basic example of how to use Hush:
package main
import (
"context"
"fmt"
"github.com/tlmanz/hush"
)
type User struct {
Name string
Password string `hush:"hide"`
Age int `hush:"mask"`
Email string `hush:"mask"`
}
func main() {
user := User{
Name: "John",
Password: "secret123",
Age: 30,
Email: "[email protected]",
}
husher := hush.NewHush()
result, err := husher.Hush(context.Background(), 10, "TESTFIELD", hush.TagHide)
if err != nil {
panic(err)
}
fmt.Println("\nString Usage Example (With Prefix):")
for _, field := range result {
fmt.Printf("%s: %s\n", field[0], field[1])
}
result, err = husher.Hush(context.Background(), 10, hush.TagHide)
if err != nil {
panic(err)
}
fmt.Println("\nString Usage Example:")
for _, field := range result {
fmt.Printf("%s\n", field[0])
}
result, err = husher.Hush(context.Background(), user)
if err != nil {
panic(err)
}
fmt.Println("\nStruct Usage Example:")
for _, field := range result {
fmt.Printf("%s: %s\n", field[0], field[1])
}
}
This will output:
String Usage Example (With Prefix):
TESTFIELD: HIDDEN
String Usage Example:
HIDDEN
Struct Usage Example:
Age: **
Email: jo************om
Name: John
Password: HIDDEN
Hush provides several options to customize its behavior:
WithSeparator(sep string)
: Set a custom separator for nested field names (default is ".")WithMaskFunc(f func(string) string)
: Set a custom masking functionWithPrivateFields(include bool)
: Include or exclude private fields in the output
There are also options we can use specific to Non Composite types like strings, maps, slices, etc.
prefix string
: Set a prefix for the field namemaskType hush.HushType (hush.TagMask or hush.TagHide)
: Set the type of masking to be applied. By default it will return the value as is.
Examples:
result, err := husher.Hush(context.Background(), data,
hush.WithSeparator("_"),
hush.WithPrivateFields(true),
hush.WithMaskFunc(func(s string) string {
return "CUSTOM_MASKED"
}),
)
result, err := husher.Hush(context.Background(), "[email protected]", "EMAIL", hush.TagMask)
By default, Hush doesn't process private (unexported) fields. You can include private fields in the output by using the WithPrivateFields
option:
result, err := husher.Hush(context.Background(), "",
hush.WithPrivateFields(true),
)
This will include private fields in the output, applying the same masking rules as public fields.
- Map keys are sorted alphabetically in the output for consistent results
- Slices and arrays are processed with index-based field names
Check out the examples
folder for more detailed usage examples:
basic_usage.go
: Demonstrates basic usage with a simple structcustom_options.go
: Shows how to use custom options like separators and masking functionscomplex_struct.go
: Illustrates handling of complex structs with nested fields, slices, and mapscustom_options_table.go
: Shows how to use custom options like separators and masking functions and display the result in a tablecustom_regex_function.go
: Shows how to use a custom regex function to mask the dataprivate_fields.go
: Shows how to include private fields in the output
To run an example:
go run examples/basic_usage.go
go run examples/custom_options.go
go run examples/complex_struct.go
go run examples/custom_options_table.go
go run examples/private_fields.go
go run examples/custom_regex_function.go
Hush is released under the MIT License. See the LICENSE file for details.