cryptography

package
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: May 5, 2022 License: MIT Imports: 4 Imported by: 0

README

cryptography

cryptography 实现了一些加密函数

Usage

aes_gcm
import "github.com/TencentBlueKing/gopkg/cryptography"

const (
	cryptoKey   = "C4QSNKR4GNPIZAH3B0RPWAIV29E7QZ66"
	aesGcmNonce = "KC9DvYrNGnPW"
)

c, err := cryptography.NewAESGcm([]byte(cryptoKey), []byte(aesGcmNonce))
if err != nil {
    return nil, fmt.Errorf("cryptos key error: %w", err)
}

plain := []byte("hello world")

// plain
cs := c.Encrypt(plain)
ds, err := c.Decrypt(cs)

// base64
cs1 := c.EncryptToString(plain)
ds1, err := c.DecryptString(cs1)

Documentation

Index

Constants

View Source
const (
	ValidAES128KeySize int = 16
	ValidAES256KeySize int = 32

	NonceByteSize int = 12
)

Variables

View Source
var (
	ErrInvalidKey   = errors.New("invalid key, should be 16 or 32 bytes")
	ErrInvalidNonce = errors.New("invalid nonce, should be 12 bytes")
)

Functions

This section is empty.

Types

type AESGcm

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

func NewAESGcm

func NewAESGcm(key []byte, nonce []byte) (aesGcm *AESGcm, err error)

NewAESGcm returns a new AES-GCM instance

func (*AESGcm) Decrypt

func (a *AESGcm) Decrypt(encryptedText []byte) ([]byte, error)

Decrypt decrypts ciphertext

func (*AESGcm) DecryptString

func (a *AESGcm) DecryptString(encryptedText string) ([]byte, error)

DecryptString decrypts ciphertext string

func (*AESGcm) Encrypt

func (a *AESGcm) Encrypt(plaintext []byte) []byte

Encrypt encrypts plaintext

func (*AESGcm) EncryptToString

func (a *AESGcm) EncryptToString(plaintext []byte) string

EncryptToString encrypts plaintext to string

type Crypto

type Crypto interface {
	Encrypt(plaintext []byte) []byte
	Decrypt(encryptedText []byte) ([]byte, error)

	EncryptToString(plaintext []byte) string
	DecryptString(encryptedText string) ([]byte, error)
}

Jump to

Keyboard shortcuts

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