secret

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package secret provides a type that cannot accidentally be written to a log.

Background: threat T-18 (secrets in logs). A `%+v` on a configuration struct is the most common way credentials end up in log files. A dedicated type with a masking String() makes that mistake impossible instead of merely forbidding it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bytes

type Bytes struct {
	// contains filtered or unexported fields
}

Bytes wraps confidential binary material - a key, a derived key, a nonce that must not be reused - the way Secret wraps a confidential string.

A separate type rather than Secret over a string, because key material is not text: converting it to a string to store it costs a copy that no longer masks, invites a comparison with `==`, and makes a length check read as a character count. The two exist side by side for the same reason threat T-18 exists at all - a `%+v` over a struct is how secrets reach log files, and a type that cannot print itself makes that mistake impossible rather than merely forbidden.

func NewBytes

func NewBytes(v []byte) Bytes

NewBytes takes ownership of the slice. The caller must not keep or reuse it: two references to one key are two places it can be overwritten from, and the copy this would otherwise make would leave the original lying about in the caller's frame either way.

func (Bytes) Equal

func (b Bytes) Equal(other Bytes) bool

Equal compares in constant time. The obvious `bytes.Equal` returns as soon as two bytes differ, which over repeated calls tells an attacker how much of a guess was right (security.md §8).

func (Bytes) GoString

func (b Bytes) GoString() string

func (Bytes) IsEmpty

func (b Bytes) IsEmpty() bool

func (Bytes) Len

func (b Bytes) Len() int

Len is the length of the material. Safe to log, and the one thing about a key that is: "the key is 16 bytes where 32 were expected" is what an operator needs in order to fix a configuration.

func (Bytes) MarshalJSON

func (b Bytes) MarshalJSON() ([]byte, error)

func (Bytes) MarshalText

func (b Bytes) MarshalText() ([]byte, error)

func (Bytes) Reveal

func (b Bytes) Reveal() []byte

Reveal returns the material. Calls are deliberately conspicuous in code review, and the slice is the wrapper's own - a caller that writes into it changes the key.

func (Bytes) String

func (b Bytes) String() string

String, GoString, MarshalText and MarshalJSON all mask - which covers fmt, slog, encoding/json and most other serialisers.

type Secret

type Secret struct {
	// contains filtered or unexported fields
}

Secret wraps a confidential value. The plaintext is reachable only through Reveal.

func New

func New(v string) Secret

func (Secret) GoString

func (s Secret) GoString() string

func (Secret) IsEmpty

func (s Secret) IsEmpty() bool

func (Secret) MarshalJSON

func (s Secret) MarshalJSON() ([]byte, error)

func (Secret) MarshalText

func (s Secret) MarshalText() ([]byte, error)

func (Secret) Reveal

func (s Secret) Reveal() string

Reveal returns the plaintext. Calls are deliberately conspicuous in code review.

func (Secret) String

func (s Secret) String() string

String, GoString and MarshalText all mask - which covers fmt, slog, encoding/json and most other serialisers.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL