crypto

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package crypto provides AES-256-GCM encryption for the scratchpad.

The key is a 256-bit random value stored as a raw file. The nonce is 12 bytes of random data prepended to the ciphertext. Each write re-encrypts the entire file.

Package crypto provides AES-256-GCM encryption and decryption for the scratchpad.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decrypt

func Decrypt(key, ciphertext []byte) ([]byte, error)

Decrypt decrypts AES-256-GCM ciphertext produced by Encrypt.

Parameters:

  • key: 32-byte AES-256 key (must match the key used for encryption)
  • ciphertext: Nonce-prefixed ciphertext as produced by Encrypt

Returns:

  • []byte: Decrypted plaintext
  • error: Non-nil if key is wrong, ciphertext is too short, or authentication fails

func Encrypt

func Encrypt(key, plaintext []byte) ([]byte, error)

Encrypt encrypts plaintext with AES-256-GCM.

The returned ciphertext is formatted as:

[12-byte nonce][ciphertext + 16-byte GCM tag]

Parameters:

  • key: 32-byte AES-256 key
  • plaintext: Data to encrypt

Returns:

  • []byte: Nonce-prefixed ciphertext
  • error: Non-nil if the key is the wrong size or encryption fails

func ExpandHome added in v0.8.0

func ExpandHome(path string) string

ExpandHome expands a leading ~/ prefix to the user's home directory.

If the path does not start with "~/", it is returned unchanged. If the home directory cannot be determined, the path is returned unchanged.

Parameters:

  • path: File path that may contain a leading ~/

Returns:

  • string: Path with ~/ expanded to the home directory

func GenerateKey

func GenerateKey() ([]byte, error)

GenerateKey returns a new 256-bit random key.

Returns:

  • []byte: A 32-byte random key
  • error: Non-nil if the system random source fails

func GlobalKeyPath added in v0.8.0

func GlobalKeyPath() string

GlobalKeyPath returns the global encryption key path.

Returns ~/.ctx/.ctx.key using os.UserHomeDir. Returns an empty string if the home directory cannot be determined.

func LoadKey

func LoadKey(path string) ([]byte, error)

LoadKey reads a 32-byte key from a file.

Parameters:

  • path: Path to the key file

Returns:

  • []byte: The 32-byte key
  • error: Non-nil if the file cannot be read or is not exactly 32 bytes

func ResolveKeyPath added in v0.8.0

func ResolveKeyPath(contextDir, overridePath string) string

ResolveKeyPath determines the effective key file path.

Resolution order:

  1. overridePath if non-empty (explicit .ctxrc key_path, with tilde expansion)
  2. Project-local path if it exists (<contextDir>/.ctx.key)
  3. Global default (~/.ctx/.ctx.key)
  4. Project-local path as fallback (when home dir unavailable)

Parameters:

  • contextDir: The .context/ directory path
  • overridePath: Explicit key path from .ctxrc (may be empty)

Returns:

  • string: The resolved key file path

func SaveKey

func SaveKey(path string, key []byte) error

SaveKey writes a key to a file with mode 0600.

Parameters:

  • path: Destination file path
  • key: Key bytes to write

Returns:

  • error: Non-nil if the file cannot be written

Types

This section is empty.

Jump to

Keyboard shortcuts

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