cipher

package
v1.1.6 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package cipher provides cryptographic cipher configuration and base functionality. It supports various symmetric encryption algorithms with different block modes, padding schemes, and streaming capabilities for secure data encryption and decryption.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AesCipher

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

func NewAesCipher

func NewAesCipher(block BlockMode) (c *AesCipher)

func (*AesCipher) Decrypt added in v1.1.1

func (c *AesCipher) Decrypt(src []byte, block cipher.Block) (dst []byte, err error)

func (*AesCipher) Encrypt added in v1.1.1

func (c *AesCipher) Encrypt(src []byte, block cipher.Block) (dst []byte, err error)

func (*AesCipher) SetAAD

func (c *AesCipher) SetAAD(aad []byte)

func (*AesCipher) SetIV

func (c *AesCipher) SetIV(iv []byte)

func (*AesCipher) SetNonce

func (c *AesCipher) SetNonce(nonce []byte)

func (*AesCipher) SetPadding

func (c *AesCipher) SetPadding(padding PaddingMode)

type BlockMode

type BlockMode string

BlockMode represents the different block cipher modes of operation

const (
	CBC BlockMode = "CBC" // Cipher Block Chaining mode
	ECB BlockMode = "ECB" // Electronic Codebook mode
	CTR BlockMode = "CTR" // Counter mode
	GCM BlockMode = "GCM" // Galois/Counter Mode
	CFB BlockMode = "CFB" // Cipher Feedback mode
	OFB BlockMode = "OFB" // Output Feedback mode
)

Supported block cipher modes

type BlowfishCipher

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

func NewBlowfishCipher

func NewBlowfishCipher(block BlockMode) (c *BlowfishCipher)

func (*BlowfishCipher) Decrypt added in v1.1.1

func (c *BlowfishCipher) Decrypt(src []byte, block cipher.Block) (dst []byte, err error)

func (*BlowfishCipher) Encrypt added in v1.1.1

func (c *BlowfishCipher) Encrypt(src []byte, block cipher.Block) (dst []byte, err error)

func (*BlowfishCipher) SetAAD

func (c *BlowfishCipher) SetAAD(aad []byte)

func (*BlowfishCipher) SetIV

func (c *BlowfishCipher) SetIV(iv []byte)

func (*BlowfishCipher) SetNonce

func (c *BlowfishCipher) SetNonce(nonce []byte)

func (*BlowfishCipher) SetPadding

func (c *BlowfishCipher) SetPadding(padding PaddingMode)

type ChaCha20Cipher added in v1.1.2

type ChaCha20Cipher struct {
	Nonce []byte
	// contains filtered or unexported fields
}

func NewChaCha20Cipher added in v1.1.2

func NewChaCha20Cipher() (c *ChaCha20Cipher)

func (*ChaCha20Cipher) SetKey added in v1.1.2

func (c *ChaCha20Cipher) SetKey(key []byte)

func (*ChaCha20Cipher) SetNonce added in v1.1.2

func (c *ChaCha20Cipher) SetNonce(nonce []byte)

type ChaCha20Poly1305Cipher added in v1.1.2

type ChaCha20Poly1305Cipher struct {
	Nonce []byte
	AAD   []byte
	// contains filtered or unexported fields
}

func NewChaCha20Poly1305Cipher added in v1.1.2

func NewChaCha20Poly1305Cipher() (c *ChaCha20Poly1305Cipher)

func (*ChaCha20Poly1305Cipher) SetAAD added in v1.1.2

func (c *ChaCha20Poly1305Cipher) SetAAD(aad []byte)

func (*ChaCha20Poly1305Cipher) SetKey added in v1.1.2

func (c *ChaCha20Poly1305Cipher) SetKey(key []byte)

func (*ChaCha20Poly1305Cipher) SetNonce added in v1.1.2

func (c *ChaCha20Poly1305Cipher) SetNonce(nonce []byte)

type CreateCipherError

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

CreateCipherError represents an error that occurs during cipher creation. This error wraps the underlying error that prevented the cipher from being created successfully, such as invalid key length or unsupported cipher mode.

func (CreateCipherError) Error

func (e CreateCipherError) Error() string

Error returns a formatted error message describing the cipher creation failure. The message includes the cipher mode and the underlying error details.

type DesCipher

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

func NewDesCipher

func NewDesCipher(block BlockMode) (c *DesCipher)

func (*DesCipher) Decrypt added in v1.1.1

func (c *DesCipher) Decrypt(src []byte, block cipher.Block) (dst []byte, err error)

func (*DesCipher) Encrypt added in v1.1.1

func (c *DesCipher) Encrypt(src []byte, block cipher.Block) (dst []byte, err error)

func (*DesCipher) SetAAD

func (c *DesCipher) SetAAD(aad []byte)

func (*DesCipher) SetIV

func (c *DesCipher) SetIV(iv []byte)

func (*DesCipher) SetNonce

func (c *DesCipher) SetNonce(nonce []byte)

func (*DesCipher) SetPadding

func (c *DesCipher) SetPadding(padding PaddingMode)

type EmptyIVError

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

EmptyIVError represents an error when the initialization vector (IV) is empty for cipher modes that require an IV. This error occurs when the IV is nil or has zero length, which is not allowed for secure cipher operations.

func (EmptyIVError) Error

func (e EmptyIVError) Error() string

Error returns a formatted error message indicating that the IV cannot be empty for the specified cipher mode.

type EmptyNonceError

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

EmptyNonceError represents an error when the nonce (number used once) is empty for cipher modes that require a nonce, such as GCM mode. This error occurs when the nonce is nil or has zero length, which is required for secure authenticated encryption.

func (EmptyNonceError) Error

func (e EmptyNonceError) Error() string

Error returns a formatted error message indicating that the nonce cannot be empty for the specified cipher mode.

type InvalidIVError

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

InvalidIVError represents an error when the initialization vector (IV) length is invalid for the specified block cipher. This error occurs when the IV length does not match the required block size for the cipher.

func (InvalidIVError) Error

func (e InvalidIVError) Error() string

Error returns a formatted error message describing the invalid IV length. The message includes the cipher mode, actual IV length, and required block size.

type InvalidSrcError

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

InvalidSrcError represents an error when the source data length is invalid for the specified block cipher mode. This error occurs when the source data length is not a multiple of the block size, which is required for most block cipher operations.

func (InvalidSrcError) Error

func (e InvalidSrcError) Error() string

Error returns a formatted error message describing the invalid source data length. The message includes the cipher mode, actual source length, and required block size.

type NilBlockError

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

NilBlockError represents an error when the cipher block is nil. This error occurs when a nil cipher block is passed to encryption or decryption functions, which is not allowed for any cipher operation.

func (NilBlockError) Error

func (e NilBlockError) Error() string

Error returns a formatted error message indicating that the cipher block cannot be nil for the specified cipher mode.

type PaddingMode

type PaddingMode string

PaddingMode represents the different padding schemes available for block ciphers

const (
	No       PaddingMode = "No"        // No padding - data must be exact block size
	Zero     PaddingMode = "Zero"      // Zero padding - fills with zeros, always adds padding
	PKCS5    PaddingMode = "PKCS5"     // PKCS5 padding - RFC 2898, 8-byte blocks only
	PKCS7    PaddingMode = "PKCS7"     // PKCS7 padding - RFC 5652, variable block size
	AnsiX923 PaddingMode = "AnsiX.923" // ANSI X.923 padding - zeros + length byte
	ISO97971 PaddingMode = "ISO9797-1" // ISO/IEC 9797-1 padding method 1
	ISO10126 PaddingMode = "ISO10126"  // ISO/IEC 10126 padding - random + length byte
	ISO78164 PaddingMode = "ISO7816-4" // ISO/IEC 7816-4 padding - same as ISO9797-1
	Bit      PaddingMode = "Bit"       // Bit padding - 0x80 + zeros
)

Supported padding modes for block cipher operations

type Rc4Cipher

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

func NewRc4Cipher

func NewRc4Cipher() (c *Rc4Cipher)

func (*Rc4Cipher) SetKey

func (c *Rc4Cipher) SetKey(key []byte)

type Salsa20Cipher added in v1.1.4

type Salsa20Cipher struct {
	Nonce []byte // 8-byte nonce for Salsa20
	// contains filtered or unexported fields
}

Salsa20Cipher represents a Salsa20 cipher configuration. Salsa20 is a stream cipher that uses a 32-byte key and 8-byte nonce.

func NewSalsa20Cipher added in v1.1.4

func NewSalsa20Cipher() *Salsa20Cipher

NewSalsa20Cipher creates a new Salsa20 cipher instance.

func (*Salsa20Cipher) SetKey added in v1.1.4

func (c *Salsa20Cipher) SetKey(key []byte)

func (*Salsa20Cipher) SetNonce added in v1.1.4

func (c *Salsa20Cipher) SetNonce(nonce []byte)

SetNonce sets the nonce for Salsa20 encryption/decryption. The nonce must be exactly 8 bytes for Salsa20.

type TeaCipher

type TeaCipher struct {
	Rounds int
	// contains filtered or unexported fields
}

func NewTeaCipher

func NewTeaCipher(block BlockMode) (c *TeaCipher)

func (*TeaCipher) Decrypt added in v1.1.6

func (c *TeaCipher) Decrypt(src []byte, block cipher.Block) (dst []byte, err error)

func (*TeaCipher) Encrypt added in v1.1.6

func (c *TeaCipher) Encrypt(src []byte, block cipher.Block) (dst []byte, err error)

func (*TeaCipher) SetAAD added in v1.1.6

func (c *TeaCipher) SetAAD(aad []byte)

func (*TeaCipher) SetIV added in v1.1.6

func (c *TeaCipher) SetIV(iv []byte)

func (*TeaCipher) SetNonce added in v1.1.6

func (c *TeaCipher) SetNonce(nonce []byte)

func (*TeaCipher) SetPadding added in v1.1.6

func (c *TeaCipher) SetPadding(padding PaddingMode)

func (*TeaCipher) SetRounds

func (c *TeaCipher) SetRounds(rounds int)

type TripleDesCipher

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

func New3DesCipher

func New3DesCipher(block BlockMode) (c *TripleDesCipher)

func (*TripleDesCipher) Decrypt added in v1.1.1

func (c *TripleDesCipher) Decrypt(src []byte, block cipher.Block) (dst []byte, err error)

func (*TripleDesCipher) Encrypt added in v1.1.1

func (c *TripleDesCipher) Encrypt(src []byte, block cipher.Block) (dst []byte, err error)

func (*TripleDesCipher) SetAAD

func (c *TripleDesCipher) SetAAD(aad []byte)

func (*TripleDesCipher) SetIV

func (c *TripleDesCipher) SetIV(iv []byte)

func (*TripleDesCipher) SetNonce

func (c *TripleDesCipher) SetNonce(nonce []byte)

func (*TripleDesCipher) SetPadding

func (c *TripleDesCipher) SetPadding(padding PaddingMode)

type TwofishCipher added in v1.1.4

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

func NewTwofishCipher added in v1.1.4

func NewTwofishCipher(block BlockMode) (c *TwofishCipher)

func (*TwofishCipher) Decrypt added in v1.1.4

func (c *TwofishCipher) Decrypt(src []byte, block cipher.Block) (dst []byte, err error)

func (*TwofishCipher) Encrypt added in v1.1.4

func (c *TwofishCipher) Encrypt(src []byte, block cipher.Block) (dst []byte, err error)

func (*TwofishCipher) SetAAD added in v1.1.4

func (c *TwofishCipher) SetAAD(aad []byte)

func (*TwofishCipher) SetIV added in v1.1.4

func (c *TwofishCipher) SetIV(iv []byte)

func (*TwofishCipher) SetNonce added in v1.1.4

func (c *TwofishCipher) SetNonce(nonce []byte)

func (*TwofishCipher) SetPadding added in v1.1.4

func (c *TwofishCipher) SetPadding(padding PaddingMode)

type XteaCipher added in v1.1.6

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

func NewXteaCipher added in v1.1.6

func NewXteaCipher(block BlockMode) (c *XteaCipher)

func (*XteaCipher) Decrypt added in v1.1.6

func (c *XteaCipher) Decrypt(src []byte, block cipher.Block) (dst []byte, err error)

func (*XteaCipher) Encrypt added in v1.1.6

func (c *XteaCipher) Encrypt(src []byte, block cipher.Block) (dst []byte, err error)

func (*XteaCipher) SetAAD added in v1.1.6

func (c *XteaCipher) SetAAD(aad []byte)

func (*XteaCipher) SetIV added in v1.1.6

func (c *XteaCipher) SetIV(iv []byte)

func (*XteaCipher) SetNonce added in v1.1.6

func (c *XteaCipher) SetNonce(nonce []byte)

func (*XteaCipher) SetPadding added in v1.1.6

func (c *XteaCipher) SetPadding(padding PaddingMode)

Source Files

  • 3des.go
  • aes.go
  • block.go
  • blowfish.go
  • chacha20.go
  • chacha20poly1305.go
  • cipher.go
  • des.go
  • errors.go
  • padding.go
  • rc4.go
  • salsa20.go
  • tea.go
  • twofish.go
  • xtea.go

Jump to

Keyboard shortcuts

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