krypto

package
v1.0.0-dev.11 Latest Latest
Warning

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

Go to latest
Published: May 1, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// SecretMarker is a string we can look for in logs to see if the app
	// is accidentally exposing secrets.
	SecretMarker = "<!SECRET_REDACTED!>"
)

Variables

View Source
var (
	// ErrUnknownKey indicates that the key used to encrypt the data is unknown.
	ErrUnknownKey = errors.New("unknown key")
	// ErrInvalidData indicates that the data is invalid.
	ErrInvalidData = errors.New("invalid data")
)
View Source
var (
	// ErrInvalidInput indicates invalid input was provided.
	ErrInvalidInput = errors.New("invalid input")
)
View Source
var (
	ErrInvalidKey = errors.New("invalid key")
)
View Source
var ErrInvalidToken = errors.New("invalid token")

Functions

This section is empty.

Types

type Argon2Hash

type Argon2Hash struct {
	Variant     string
	Version     uint32
	MemoryKiB   uint32
	Iterations  uint32
	Parallelism uint8
	Salt        []byte
	Hash        []byte
}

Argon2Hash is a hash generated by the Argon2 Hashing Algorithm.

func HashArgon2

func HashArgon2(b []byte) (Argon2Hash, error)

HashArgon2 hashes a byte slice using the argon2id algorithm.

func HashArgon2WithKey

func HashArgon2WithKey(b []byte, salt Key) (Argon2Hash, error)

HashArgon2WithKey hashes a byte slice using the argon2id algorithm and uses the provided key as a salt.

func ParseArgon2Hash

func ParseArgon2Hash(txt string) (Argon2Hash, error)

ParseArgon2Hash parses an argon2 hash from the string representation provided by the String method.

func (Argon2Hash) MarshalText

func (h Argon2Hash) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (Argon2Hash) MatchBytes

func (h Argon2Hash) MatchBytes(b []byte) bool

MatchBytes checks if the hash matches the given byte slice.

func (*Argon2Hash) Scan

func (h *Argon2Hash) Scan(v any) error

Scan implements the sql.Scanner interface.

func (Argon2Hash) String

func (h Argon2Hash) String() string

String returns the string representation of the hash.

func (*Argon2Hash) UnmarshalText

func (h *Argon2Hash) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

type Encryptor

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

Encryptor encrypts and decrypts data using AES-GCM.

The encryptor uses an append only list of keys for encryption and decryption. The last key in the list is considered the latest key.

To construct output data, the encryptor prefixes the encrypted data with the index of the used key. This allows the encryptor to work with multiple keys and to decrypt data encrypted with an older key.

The index used is not considered secret.

func NewEncryptor

func NewEncryptor(keys []Key) (*Encryptor, error)

NewEncryptor creates a new encryptor with the provided keys.

func (*Encryptor) Decrypt

func (s *Encryptor) Decrypt(message []byte) ([]byte, error)

Decrypt decrypts the data using the key identified by the first 4 bytes in the data. It returns the decrypted data or an error.

func (*Encryptor) Encrypt

func (s *Encryptor) Encrypt(data []byte) ([]byte, error)

Encrypt encrypts the data using the latest available key. It returns the encrypted data prefixed with the key identifier.

type Key

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

Key is 32-byte encryption key.

func ParseKey

func ParseKey(raw string) (Key, error)

ParseKey expects a hex encoded key of 32 bytes (64 bytes as hex).

func (Key) Format

func (k Key) Format(f fmt.State, verb rune)

func (Key) MarshalText

func (k Key) MarshalText() ([]byte, error)

func (Key) SecretValue

func (k Key) SecretValue() []byte

SecretValue returns the key as a byte slice. This is provided as an escape hatch for cases where the key needs to be provided to third party packages or libraries.

type Secret

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

SecretMarker is arbitrary sensitive data that needs to be passed around but not exposed. Things like API keys or other credentials.

func NewSecret

func NewSecret(raw string) Secret

NewSecret creates a new secret.

func (Secret) Format

func (k Secret) Format(f fmt.State, verb rune)

func (Secret) MarshalText

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

func (Secret) SecretValue

func (k Secret) SecretValue() []byte

SecretValue returns the secret as a byte slice. This is provided as an escape hatch for cases where the key needs to be provided to third party packages or libraries.

type Token

type Token [tokenLen]byte

Token is a random token that is sent via email.

The only time a token should be provided in plaintext is as part of the email to the user. Tokens are confidential and should never be exposed in logs or persisted in plaintext.

func GenerateToken

func GenerateToken() (Token, error)

GenerateToken creates a new random token.

func ParseToken

func ParseToken(raw string) (Token, error)

ParseToken parses a token from a string.

func (Token) LogValue

func (t Token) LogValue() slog.Value

LogValue implements the slog.Valuer interface.

func (Token) String

func (t Token) String() string

String returns the string representation of the token. As opposed to a Password this allowed, we need to embed the token in emails.

func (*Token) UnmarshalText

func (t *Token) UnmarshalText(text []byte) error

Jump to

Keyboard shortcuts

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