crypto

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2026 License: Apache-2.0 Imports: 10 Imported by: 229

Documentation

Index

Constants

View Source
const (
	AEADAlgorithmAES256GCM         AEADAlgorithm = "aes-256-gcm"
	AEADAlgorithmXChaCha20Poly1305 AEADAlgorithm = "xchacha20-poly1305"

	AEADKeySize               = 32
	DefaultAEADMaxPayloadSize = 64 * 1024
)

Variables

View Source
var DefaultSalt = "crypto"

Functions

func Decode added in v0.3.0

func Decode(s, key []byte) ([]byte, error)

decode bytes by aes cfb

func Encode added in v0.3.0

func Encode(s, key []byte) ([]byte, error)

Types

type AEADAlgorithm added in v0.7.0

type AEADAlgorithm string

type AEADStreamOptions added in v0.7.0

type AEADStreamOptions struct {
	Algorithm      AEADAlgorithm
	Key            []byte
	MaxPayloadSize int
}

AEADStreamOptions configures the framed AEAD stream reader and writer. Key must be a raw 32-byte AEAD key. Callers are responsible for deriving it from application secrets before constructing the stream.

The AEAD stream authenticates each frame and its order, but it does not authenticate end-of-stream. A clean EOF on a frame boundary is treated as normal stream termination. Protocols that need object/file truncation detection must authenticate a total length or final record at a higher layer.

For AES-256-GCM, this package enforces a per-stream limit of 2^32 frames. This is a local limit only; callers that reuse a key across multiple streams or directions must enforce the global per-key limit themselves or derive independent keys.

type AEADStreamReader added in v0.7.0

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

AEADStreamReader decrypts the framed AEAD stream produced by AEADStreamWriter.

It authenticates each frame and its order using the stream nonce, frame header, and incrementing frame nonce. Truncation inside a frame is returned as an error from the underlying reader, but EOF at a frame boundary is treated as normal stream termination and does not authenticate end-of-stream. AEADStreamReader is not safe for concurrent use by multiple goroutines.

func NewAEADStreamReader added in v0.7.0

func NewAEADStreamReader(r io.Reader, opts AEADStreamOptions) (*AEADStreamReader, error)

NewAEADStreamReader returns an io.Reader that decrypts framed AEAD records. It validates frame authentication and ordering, but EOF at a frame boundary is returned as a normal EOF; see AEADStreamOptions for end-of-stream behavior.

func (*AEADStreamReader) Read added in v0.7.0

func (r *AEADStreamReader) Read(p []byte) (nRet int, errRet error)

type AEADStreamWriter added in v0.7.0

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

AEADStreamWriter encrypts plaintext into a framed AEAD stream.

The wire format is:

stream nonce || repeated frame

Each frame is:

uint32 ciphertext length || AEAD ciphertext and tag

The stream nonce is sent in cleartext and seeds the first frame nonce. Each subsequent frame increments that nonce by one. Each frame authenticates the stream nonce and frame length header as AAD, which binds frame order to the stream. AEADStreamWriter is not safe for concurrent use by multiple goroutines. Once Write returns an error, the writer remembers it and returns the same error from subsequent Write calls.

func NewAEADStreamWriter added in v0.7.0

func NewAEADStreamWriter(w io.Writer, opts AEADStreamOptions) (*AEADStreamWriter, error)

NewAEADStreamWriter returns an io.Writer that encrypts bytes into framed AEAD records. It is intended for connection-oriented streams. Close/final-record semantics are not part of this writer; see AEADStreamOptions for end-of-stream behavior.

func (*AEADStreamWriter) Write added in v0.7.0

func (w *AEADStreamWriter) Write(p []byte) (nRet int, errRet error)

type Reader

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

Reader is an io.Reader that can read encrypted bytes. Now it only supports aes-128-cfb.

func NewReader

func NewReader(r io.Reader, key []byte) *Reader

NewReader returns a new Reader that decrypts bytes from r

func (*Reader) Read

func (r *Reader) Read(p []byte) (nRet int, errRet error)

Read satisfies the io.Reader interface.

type Writer

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

Writer is an io.Writer that can write encrypted bytes. Now it only support aes-128-cfb.

func NewWriter

func NewWriter(w io.Writer, key []byte) (*Writer, error)

NewWriter returns a new Writer that encrypts bytes to w.

func (*Writer) Write

func (w *Writer) Write(p []byte) (nRet int, errRet error)

Write satisfies the io.Writer interface.

Jump to

Keyboard shortcuts

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