Help us shape the future of Go! Take the 2026 Go Developer Survey

cipher

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Overview

Package cipher provides symmetric encryption using XChaCha20-Poly1305. It is designed for encrypting sensitive data before storing in databases.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCiphertextTooShort is returned when the ciphertext is shorter than the nonce size.
	ErrCiphertextTooShort = errors.New("cipher: ciphertext too short")
)

Functions

This section is empty.

Types

type Cipher

type Cipher struct {
	AEAD cipher.AEAD
}

Cipher wraps XChaCha20-Poly1305 AEAD for encryption and decryption.

func New

func New(key string) (*Cipher, error)

New creates a new Cipher with the given key. The key must be exactly 32 bytes for XChaCha20-Poly1305.

func (*Cipher) Decode

func (x *Cipher) Decode(ciphertext string) ([]byte, error)

Decode decrypts a base64-encoded ciphertext and returns the original plaintext. Returns ErrCiphertextTooShort if the ciphertext is invalid.

func (*Cipher) Encode

func (x *Cipher) Encode(data []byte) (string, error)

Encode encrypts the plaintext data and returns a base64-encoded ciphertext. A random nonce is generated for each encryption and prepended to the ciphertext.

Jump to

Keyboard shortcuts

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