wcrypto

package module
v0.0.0-...-2bfd647 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2022 License: Apache-2.0 Imports: 19 Imported by: 0

README

wcrypto

GitHub Repo stars GitHub GitHub go.mod Go version GitHub all releases GitHub CI Status GitHub Release Status Go Report Card Go.Dev reference codecov

Example

package main

import (
	"fmt"
	"github.com/wyy-go/wcrypto"
)

func main() {
	key := "1234567899874563"
	iv := "qwertyuiopasdfgj"
	testText := "hello world"

	crypto, err := wcrypto.NewChipher(wcrypto.Aes128Cbc, key, iv)
	if err != nil {
		fmt.Println(err)
		return
	}
	cipherText, err := crypto.Encrypt(testText)
	if err != nil {
		fmt.Println(err)
		return
	}

	plainText, err := crypto.Decrypt(cipherText)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(plainText)

}


Reference

需要Padding的有:CBC(,PCBC也需要,本文未涉及该加密模式)、ECB。 不需要Padding的有:CFB、OFB、CTR。

以上五种分组模式中,ECB模式很容易被破解,如今已经很少再使用,其余四种分组模式各有千秋。 但极力推荐CBC模式和CTR模式,尤其是CTR模式,不需要填充,代码实现起来很方便。 而且加密和解密的方法是一样的,并且可以实现并发分组,效率高,安全性也有保障

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewBlowfishCipher

func NewBlowfishCipher(key []byte) (cipher.Block, error)

NewBlowfishCipher new blowfish cipher The key argument should be the Blowfish key, from 1 to 56 bytes.

func NewCBCDecrypter

func NewCBCDecrypter(block cipher.Block, iv []byte) cipher.Stream

func NewCBCEncrypter

func NewCBCEncrypter(block cipher.Block, iv []byte) cipher.Stream

func NewCast5Cipher

func NewCast5Cipher(key []byte) (cipher.Block, error)

NewCast5Cipher new cast5 cipher, The key size should 32

func NewChacha20

func NewChacha20(key, iv []byte) (cipher.Stream, error)

NewChacha20 new chacha20 key size should 32, iv size should one of 12,24

func NewRc4Md5

func NewRc4Md5(key, iv []byte) (cipher.Stream, error)

NewRc4Md5 new rc4-md5 key size should 16, iv size should one of 6,16

func NewSalsa20

func NewSalsa20(key, iv []byte) (cipher.Stream, error)

NewSalsa20 new salsa20 key size should 32, iv size should one of 8

func NewTwofishCipher

func NewTwofishCipher(key []byte) (cipher.Block, error)

NewTwofishCipher new twofish cipher The key argument should be the Twofish key, 16, 24 or 32 bytes.

func NewXteaCipher

func NewXteaCipher(key []byte) (cipher.Block, error)

NewXteaCipher new xtea cipher The key argument should be the XTEA key. XTEA only supports 128 bit (16 byte) keys.

func RandStringBytes

func RandStringBytes(n int) string

Types

type AnsiX923Padding

type AnsiX923Padding struct {
}

func (*AnsiX923Padding) Padding

func (p *AnsiX923Padding) Padding(plaintext []byte, blockSize int) ([]byte, error)

Padding ANSI X.923

func (*AnsiX923Padding) UnPadding

func (p *AnsiX923Padding) UnPadding(ciphertext []byte) ([]byte, error)

type Base64

type Base64 struct{}

func (*Base64) Decode

func (c *Base64) Decode(ciphertext string) ([]byte, error)

func (*Base64) Encode

func (c *Base64) Encode(plaintext []byte) string

type BlockMode

type BlockMode string
const (
	BlockModeCBC BlockMode = "CBC"
	BlockModeCTR BlockMode = "CTR"
	BlockModeOFB BlockMode = "OFB"
	BlockModeCFB BlockMode = "CFB"
)

type Cipher

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

func (*Cipher) Decrypt

func (c *Cipher) Decrypt(src string) (string, error)

func (*Cipher) Encrypt

func (c *Cipher) Encrypt(plainText string) (string, error)

type Codec

type Codec interface {
	Encode(plaintext []byte) string
	Decode(ciphertext string) ([]byte, error)
}

type CodecType

type CodecType string
const (
	CodecTypeNone   CodecType = "None"
	CodecTypeBase64 CodecType = "Base64"
	CodecTypeHex    CodecType = "Hex"
)

type Crypto

type Crypto interface {
	Encrypt(string) (string, error)
	Decrypt(string) (string, error)
}

func NewChipher

func NewChipher(method CryptoMethod, key, iv string, opts ...Option) (Crypto, error)

type CryptoMethod

type CryptoMethod string
const (
	Aes128Cbc     CryptoMethod = "aes-128-cbc"
	Aes192Cbc     CryptoMethod = "aes-192-cbc"
	Aes256Cbc     CryptoMethod = "aes-256-cbc"
	Aes128Cfb     CryptoMethod = "aes-128-cfb"
	Aes192Cfb     CryptoMethod = "aes-192-cfb"
	Aes256Cfb     CryptoMethod = "aes-256-cfb"
	Aes128Ctr     CryptoMethod = "aes-128-ctr"
	Aes192Ctr     CryptoMethod = "aes-192-ctr"
	Aes256Ctr     CryptoMethod = "aes-256-ctr"
	Aes128Ofb     CryptoMethod = "aes-128-ofb"
	Aes192Ofb     CryptoMethod = "aes-192-ofb"
	Aes256Ofb     CryptoMethod = "aes-256-ofb"
	DesCbc        CryptoMethod = "des-cbc"
	DesCfb        CryptoMethod = "des-cfb"
	DesCtr        CryptoMethod = "des-ctr"
	DesOfb        CryptoMethod = "des-ofb"
	Des3Cfb       CryptoMethod = "3des-cfb"
	Des3Ctr       CryptoMethod = "3des-ctr"
	Des3Ofb       CryptoMethod = "3des-ofb"
	BlowfishCbc   CryptoMethod = "blowfish-cbc"
	BlowfishCfb   CryptoMethod = "blowfish-cfb"
	BlowfishCtr   CryptoMethod = "blowfish-ctr"
	BlowfishOfb   CryptoMethod = "blowfish-ofb"
	Cast5Cbc      CryptoMethod = "cast5-cbc"
	Cast5Cfb      CryptoMethod = "cast5-cfb"
	Cast5Ctr      CryptoMethod = "cast5-ctr"
	Cast5Ofb      CryptoMethod = "cast5-ofb"
	Twofish128CBC CryptoMethod = "twofish-128-cbc"
	Twofish192CBC CryptoMethod = "twofish-192-cbc"
	Twofish256CBC CryptoMethod = "twofish-256-cbc"
	Twofish128Cfb CryptoMethod = "twofish-128-cfb"
	Twofish192Cfb CryptoMethod = "twofish-192-cfb"
	Twofish256Cfb CryptoMethod = "twofish-256-cfb"
	Twofish128Ctr CryptoMethod = "twofish-128-ctr"
	Twofish192Ctr CryptoMethod = "twofish-192-ctr"
	Twofish256Ctr CryptoMethod = "twofish-256-ctr"
	Twofish128Ofb CryptoMethod = "twofish-128-ofb"
	Twofish192Ofb CryptoMethod = "twofish-192-ofb"
	Twofish256Ofb CryptoMethod = "twofish-256-ofb"
	XteaCbc       CryptoMethod = "xtea-cbc"
	XteaCfb       CryptoMethod = "xtea-cfb"
	XteaCtr       CryptoMethod = "xtea-ctr"
	XteaOfb       CryptoMethod = "xtea-ofb"
	TeaCbc        CryptoMethod = "tea-cbc"
	TeaCfb        CryptoMethod = "tea-cfb"
	TeaCtr        CryptoMethod = "tea-ctr"
	TeaOfb        CryptoMethod = "tea-ofb"
	Rc4Md5        CryptoMethod = "rc4-md5"
	Rc4Md5_6      CryptoMethod = "rc4-md5-6"
	Chacha20      CryptoMethod = "chacha20"
	Chacha20_Ietf CryptoMethod = "chacha20-ietf"
	Salsa20       CryptoMethod = "salsa20"
)

type Hex

type Hex struct{}

func (*Hex) Decode

func (c *Hex) Decode(ciphertext string) ([]byte, error)

func (*Hex) Encode

func (c *Hex) Encode(plaintext []byte) string

type ISO10126Padding

type ISO10126Padding struct {
}

func (*ISO10126Padding) Padding

func (p *ISO10126Padding) Padding(plaintext []byte, blockSize int) ([]byte, error)

Padding ISO10126 implements ISO 10126 byte padding. This has been withdrawn in 2007.

func (*ISO10126Padding) UnPadding

func (p *ISO10126Padding) UnPadding(ciphertext []byte) ([]byte, error)

type ISO97971Padding

type ISO97971Padding struct {
}

func (*ISO97971Padding) Padding

func (p *ISO97971Padding) Padding(plaintext []byte, blockSize int) ([]byte, error)

func (*ISO97971Padding) UnPadding

func (p *ISO97971Padding) UnPadding(ciphertext []byte) ([]byte, error)

type IvSizeError

type IvSizeError int

IvSizeError iv size error

func (IvSizeError) Error

func (i IvSizeError) Error() string

Error implement Error interface

type KeyIv

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

func (KeyIv) CheckLen

func (k KeyIv) CheckLen(key, iv string) error

func (*KeyIv) Iv

func (k *KeyIv) Iv() []byte

func (*KeyIv) IvLen

func (k *KeyIv) IvLen() int

func (*KeyIv) Key

func (k *KeyIv) Key() []byte

func (*KeyIv) KeyLen

func (k *KeyIv) KeyLen() int

type KeySizeError

type KeySizeError int

KeySizeError key size error

func (KeySizeError) Error

func (k KeySizeError) Error() string

Error implement Error interface

type Mode

type Mode interface {
	Encrypt(block cipher.Block, data, iv []byte) ([]byte, error)
	Decrypt(block cipher.Block, data, iv []byte) ([]byte, error)
}

type NewCipher

type NewCipher func(key []byte) (cipher.Block, error)

type NewStream

type NewStream func(key, iv []byte) (cipher.Stream, error)

type None

type None struct {
}

func (*None) Decode

func (c *None) Decode(ciphertext string) ([]byte, error)

func (*None) Encode

func (c *None) Encode(plaintext []byte) string

type NonePadding

type NonePadding struct {
}

func (*NonePadding) Padding

func (p *NonePadding) Padding(ciphertext []byte, blockSize int) ([]byte, error)

func (*NonePadding) UnPadding

func (p *NonePadding) UnPadding(origData []byte) ([]byte, error)

type Option

type Option func(*Options)

func WithCodec

func WithCodec(code CodecType) Option

func WithMode

func WithMode(bm BlockMode) Option

func WithPadding

func WithPadding(padding PaddingType) Option

type Options

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

type PKCS5Padding

type PKCS5Padding struct {
}

func (*PKCS5Padding) Padding

func (p *PKCS5Padding) Padding(ciphertext []byte, blockSize int) ([]byte, error)

func (*PKCS5Padding) UnPadding

func (p *PKCS5Padding) UnPadding(origData []byte) ([]byte, error)

type PKCS7Padding

type PKCS7Padding struct {
}

func (*PKCS7Padding) Padding

func (p *PKCS7Padding) Padding(plainText []byte, blockSize int) ([]byte, error)

Padding PKCS7

func (*PKCS7Padding) UnPadding

func (p *PKCS7Padding) UnPadding(origData []byte) ([]byte, error)

type Padding

type Padding interface {
	Padding(ciphertext []byte, blockSize int) ([]byte, error)
	UnPadding(origData []byte) ([]byte, error)
}

type PaddingType

type PaddingType string
const (
	PaddingTypeNone     PaddingType = "NonePadding"
	PaddingTypeAnsiX923 PaddingType = "AnsiX923Padding"
	PaddingTypeISO10126 PaddingType = "ISO10126Padding"
	PaddingTypeISO97971 PaddingType = "ISO97971Padding"
	PaddingTypePKCS7    PaddingType = "PKCS7Padding"
	PaddingTypePKCS5    PaddingType = "PKCS5Padding"
	PaddingTypeZero     PaddingType = "ZeroPadding"
)

type UnsupportedError

type UnsupportedError string

func (UnsupportedError) Error

func (s UnsupportedError) Error() string

Error implement Error interface

type ZeroPadding

type ZeroPadding struct {
}

ZeroPadding 使用0填充有个缺点,当元数据尾部也存在0时,在unpadding时可能会存在问题

func (*ZeroPadding) Padding

func (p *ZeroPadding) Padding(ciphertext []byte, blockSize int) ([]byte, error)

func (*ZeroPadding) UnPadding

func (p *ZeroPadding) UnPadding(origData []byte) ([]byte, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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