secutil

package
v0.7.5 Latest Latest
Warning

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

Go to latest
Published: May 1, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package secutil provide some security utils

Index

Constants

View Source
const (
	CryptAes128CBC = "aes-128-cbc"
	CryptAes256CBC = "aes-256-cbc"
)

aes methods

View Source
const (
	PadTypeNone uint8 = iota
	PadTypeZeros
	PadTypePKCS5
	PadTypePKCS7
)

padding type for encoding contents.

Variables

View Source
var (
	// ErrInvalidAESKeySize means the AES key length is not 16, 24 or 32 bytes.
	ErrInvalidAESKeySize = errors.New("secutil: invalid AES key size")
	// ErrInvalidGCMNonceSize means the GCM nonce length does not match AEAD requirements.
	ErrInvalidGCMNonceSize = errors.New("secutil: invalid GCM nonce size")
)
View Source
var ErrUnPadding = errors.New("un-padding decrypted data fail")

ErrUnPadding error

Functions

func DecryptGCM added in v0.7.5

func DecryptGCM(key, nonce, ciphertext []byte) ([]byte, error)

DecryptGCM decrypts ciphertext with AES-GCM.

func EncryptGCM added in v0.7.5

func EncryptGCM(key, plaintext []byte) (ciphertext, nonce []byte, err error)

EncryptGCM encrypts plaintext with AES-GCM.

The key length must be 16, 24 or 32 bytes. A random nonce is generated and returned alongside the ciphertext.

func PKCS5Padding

func PKCS5Padding(ciphertext []byte, blockSize int) []byte

PKCS5Padding input data

func PKCS5UnPadding

func PKCS5UnPadding(origData []byte) ([]byte, error)

PKCS5UnPadding input data

func PKCS7Padding

func PKCS7Padding(ciphertext []byte, blockSize int) []byte

PKCS7Padding input data

func PKCS7UnPadding

func PKCS7UnPadding(origData []byte) ([]byte, error)

PKCS7UnPadding input data

Types

type AesCrypt

type AesCrypt struct {
	CryptConfig
	// contains filtered or unexported fields
}

AesCrypt struct

func NewAesCrypt

func NewAesCrypt() *AesCrypt

NewAesCrypt instance

func (*AesCrypt) Config

func (p *AesCrypt) Config(fn func(c *CryptConfig)) *AesCrypt

Config crypt instance

func (*AesCrypt) Decrypt

func (p *AesCrypt) Decrypt(enc []byte) ([]byte, error)

Decrypt an encrypt to source data

func (*AesCrypt) DecryptString

func (p *AesCrypt) DecryptString(enc string) (string, error)

DecryptString to source string. return error on fail.

func (*AesCrypt) Encrypt

func (p *AesCrypt) Encrypt(src []byte) ([]byte, error)

Encrypt input source bytes. return error on fail.

func (*AesCrypt) EncryptString

func (p *AesCrypt) EncryptString(src string) (string, error)

EncryptString to encoded string. return error on fail.

func (*AesCrypt) Init

func (p *AesCrypt) Init() error

Init crypt instance

type CryptConfig

type CryptConfig struct {
	Key string
	// IV string, length must be equals to cipher.Block.BlockSize()
	IV string
	// Method name. eg: CryptAes256CBC
	Method string
	// PadType for padding string.
	PadType uint8
	Encoder byteutil.BytesEncoder
}

CryptConfig struct

Jump to

Keyboard shortcuts

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