Documentation
ΒΆ
Overview ΒΆ
secrecy is a simple, safe library which provides wrapper type for secret management in Go.
Index ΒΆ
- Variables
- func UnsafeBytesToString(bytes []byte) string
- func UnsafeStringToBytes(str string) []byte
- func Zeroize(v any)
- type Secret
- func (s *Secret[T]) DisableZeroize()
- func (s *Secret[T]) EnableZeroize()
- func (s *Secret[T]) ExposeSecret() T
- func (s Secret[T]) GoString() string
- func (s Secret[T]) MarshalText() ([]byte, error)
- func (s Secret[T]) String() string
- func (s *Secret[T]) UnmarshalJSON(data []byte) error
- func (s Secret[T]) Zeroize()
- type SecretExposer
- type SecretString
- type SerializableSecret
- type Zeroizer
Constants ΒΆ
This section is empty.
Variables ΒΆ
var SecretLeakedMarker = "<!SECRET_LEAKED!>"
SecretLeakedMarker is a marker string returned by Secret[T] when accidentally leaking the secret. This value is returned when you try to fmt.Println(secret) or serialize it to JSON/XML. This enable you to easily monitor your logs for secret leak.
Functions ΒΆ
func UnsafeBytesToString ΒΆ added in v0.2.0
UnsafeStringToBytes converts given string into a bytes slice using unsafe. This function doesn't perform any copy so mutating the byte slice will mutate the returned string.
func UnsafeStringToBytes ΒΆ added in v0.2.0
UnsafeStringToBytes converts given string into a bytes slice using unsafe. This function doesn't perform any copy so mutating the byte slice will mutate the string.
func Zeroize ΒΆ
func Zeroize(v any)
Zeroize recursively changes to zeros memory pointed by the given value. Panics if data is or contains a struct with non ignored unexported fields and doesn't implements Zeroizer.
This function zeroize all mutable memory, be sure to not share that memory as it may lead to unexpected behavior. All pointers (map, slice, pointers) type are emptied before being replaced by nil.
Go strings are immutable and can't be zeroized (without using unsafe), if you want your secret to be fully zeroized, store them as []byte.
Types ΒΆ
type Secret ΒΆ
type Secret[T any] struct { // contains filtered or unexported fields }
Secret is a wrapper type for values that contains secrets, which attempts to limit accidental exposure and ensure secrets are wiped from memory when garbage collected. (e.g. passwords, cryptographic keys, access tokens or other credentials)
Prefer SecretString over Secret[string] as go string are immutable and can't be wiped from memory.
func NewSecret ΒΆ
NewSecret wraps given secret and returns *Secret[T]. Wrapped value and inner values will be entirely wiped from memory when garbage collected, this includes maps, slices pointers, etc. Returned secret owns wrapped value and inner value, you must not share underlying data.
func (*Secret[T]) DisableZeroize ΒΆ
func (s *Secret[T]) DisableZeroize()
Disable zeroize on garbage collection for this secret.
func (*Secret[T]) EnableZeroize ΒΆ
func (s *Secret[T]) EnableZeroize()
Enable zeroize on garbage collection for this secret. By default zeroize is enabled, you don't need to call this function if didn't call DisableZeroize before.
func (*Secret[T]) ExposeSecret ΒΆ
func (s *Secret[T]) ExposeSecret() T
ExposeSecret returns a copy of inner secret. Don't store returned value and prefer passing Secret itself if needed.
func (Secret[T]) GoString ΒΆ added in v0.5.0
Format implements fmt.GoStringer. This function return the SecretLeakedMarker marker string and appends "Secret[T](******)" as a suffix.
func (Secret[T]) MarshalText ΒΆ added in v0.5.0
MarshalText implements encoding.TextMarshaler This function returns the SecretLeakedMarker marker byte slice so it can be easily searched.
func (Secret[T]) String ΒΆ
String implements fmt.Stringer. This function returns the SecretLeakedMarker marker string so it can be easily searched.
func (*Secret[T]) UnmarshalJSON ΒΆ added in v0.7.0
UnmarshalJSON implements json.Unmarshaler.
type SecretExposer ΒΆ
type SecretExposer[T any] interface { ExposeSecret() T }
SecretExposer define any secret wrapper types that can expose it's underlying secret of type T. Don't store returned value and prefer passing SecretExposer itself if needed.
type SecretString ΒΆ
SecretString is a wrapper around Secret[[]byte] that expose its secret as string.
func NewSecretString ΒΆ
func NewSecretString(secret []byte) SecretString
NewSecretString wraps given secret and returns SecretString. This function takes a byte slice as parameter so the secret can be wiped on garbage collection. Use UnsafeStringToBytes to convert your string to a byte slice if needed.
func (SecretString) ExposeSecret ΒΆ
func (ss SecretString) ExposeSecret() string
ExposeSecret exposes underlying secret as a string using unsafe. Don't store returned value and prefer passing SecretString itself if needed.
type SerializableSecret ΒΆ
type SerializableSecret[S any, T SecretExposer[S]] struct { // contains filtered or unexported fields }
SerializableSecret is a serializable wrapper around a SecretExposer.
func NewSerializableSecret ΒΆ
func NewSerializableSecret[S any, T SecretExposer[S]](secret T) SerializableSecret[S, T]
NewSerializableSecret wraps secret and return a SerializableSecret that implements json.Marshaler interface.
func (SerializableSecret[S, T]) ExposeSecret ΒΆ
func (ss SerializableSecret[S, T]) ExposeSecret() S
ExposeSecret implements SecretExposer.
func (SerializableSecret[S, T]) MarshalJSON ΒΆ
func (ss SerializableSecret[S, T]) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler.
func (*SerializableSecret[S, T]) UnmarshalJSON ΒΆ added in v0.7.0
func (ss *SerializableSecret[S, T]) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler.
type Zeroizer ΒΆ
type Zeroizer interface {
Zeroize()
}
Interface for securely erasing values from memory. You may want to implement it if you're struct contains unexported fields that can't be ignored using `Zeroizer:"ignore"` struct tag. This interface MUST be implemented using a struct receiver and not a pointer receiver.
