Documentation
¶
Overview ¶
Package secrecy provides helpers for handling sensitive values. When wrapped in a Secret, the original value will never be printed or encoded unless it is explicitly exposed with the Expose method.
Index ¶
- func SetRedactedString(s string)
- func Zeroize(value any)
- type Secret
- func (s Secret[T]) Expose() T
- func (s Secret[T]) GoString() string
- func (s Secret[T]) GobEncode() ([]byte, error)
- func (s Secret[T]) MarshalJSON() ([]byte, error)
- func (s Secret[T]) MarshalTOML() ([]byte, error)
- func (s Secret[T]) MarshalText() ([]byte, error)
- func (s Secret[T]) MarshalYAML() (any, error)
- func (s Secret[T]) String() string
- func (s *Secret[T]) UnmarshalJSON(data []byte) error
- func (s *Secret[T]) Zero()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetRedactedString ¶ added in v0.4.0
func SetRedactedString(s string)
SetRedactedString sets the global value used when a secret is formatted or encoded. It should be called during program initialization, before any Secrets are created. By default the string is "[REDACTED]".
Types ¶
type Secret ¶
type Secret[T any] struct { // contains filtered or unexported fields }
Secret wraps a sensitive value to prevent it from being inadvertently leaked through logging, formatting, or other encoding mechanisms. To retrieve the underlying value, the Expose method must be called.
func New ¶
New returns a new Secret that wraps the provided value.
password := secrecy.New("p@ssword")
fmt.Println(password) // prints [REDACTED]
func NewZeroizing ¶ added in v0.2.0
NewZeroizing returns a pointer to a Secret that wraps the provided value and automatically zeroes it when the Secret is garbage collected. The value should not be accessed or modified outside of the returned Secret.
token := secrecy.NewZeroizing([]byte("key"))
func (Secret[T]) Expose ¶
func (s Secret[T]) Expose() T
Expose returns the underlying secret value. This is the only way to retrieve the wrapped data.
func (Secret[T]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (Secret[T]) MarshalTOML ¶
MarshalTOML implements the toml.Marshaler interface.
func (Secret[T]) MarshalText ¶
MarshalText implements the TextMarshaler interface.
func (Secret[T]) MarshalYAML ¶
MarshalYAML implements the yaml.Marshaler interface.
func (*Secret[T]) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.