crypt

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2023 License: BSD-3-Clause Imports: 15 Imported by: 0

README

crypt

A simple Go library for cryptographic

Simple example

import "github.com/kayon/crypt"

func main() {

    ...

    c, err := crypt.NewAES(key, nil, crypt.Options{Padding: crypt.PAD_ZEROPADDING})
    if err != nil {
        ...
    }

    // encrypt
    ciphertext, err := c.Encrypt(plaintext)

    // decrypt
    plaintext, err = c.Decrypt(ciphertext)

    ...

    // shortcuts
    ciphertext, err := crypt.AES.Encrypt(plaintext, key, nil, crypt.Options{Mode: crypt.MODE_CBC, Padding: crypt.PAD_ANSIX923})

}

List of methods

AES

NewAES(key, iv []byte, args ...Options) (*Crypt, error)

DES

NewDES(key, iv []byte, args ...Options) (*Crypt, error)

Triple DES

NewDES3(key, iv []byte, args ...Options) (*Crypt, error)

ChaCha20

NewChaCha20(key, iv []byte)(*Crypt, error)

Blowfish

NewBlowfish(key[]byte) (*Crypt, error)

RC4

NewRC4(key []byte) (*Crypt, error)
Crypt
(Crypt) Encrypt(plaintext []byte) (ciphertext []byte, err error)

(Crypt) Decrypt(ciphertext []byte) (plaintext []byte, err error)

Shortcuts

AES

  • AES.Encrypt(plaintext, key, iv []byte, args ...Options) ([]byte, error)

  • AES.Decrypt(ciphertext, key, iv []byte, args ...Options) ([]byte, error)

DES

  • DES.Encrypt(plaintext, key, iv []byte, args ...Options) ([]byte, error)

  • DES.Decrypt(ciphertext, key, iv []byte, args ...Options) ([]byte, error)

DES3

  • DES3.Encrypt(plaintext, key, iv []byte, args ...Options) ([]byte, error)

  • DES3.Decrypt(ciphertext, key, iv []byte, args ...Options) ([]byte, error)

ChaCha20

  • ChaCha20.Encrypt(plaintext, key, iv []byte) ([]byte, error)

  • ChaCha20.Decrypt(ciphertext, key, iv []byte) ([]byte, error)

Blowfish

  • Blowfish.Encrypt(plaintext, key []byte) ([]byte, error)

  • Blowfish.Decrypt(ciphertext, key []byte) ([]byte, error)

RC4

  • RC4.Encrypt(plaintext, key []byte) ([]byte, error)

  • RC4.Decrypt(ciphertext, key []byte) ([]byte, error)

MD5

  • MD5.Sum(plaintext []byte) []byte

  • MD5.Hex(plaintext []byte) string

Sha3

  • Sha3.Sum224(data []byte) []byte

  • Sha3.Sum256(data []byte) []byte

  • Sha3.Sum384(data []byte) []byte

  • Sha3.Sum512(data []byte) []byte

  • Sha3.Shake128(data []byte, size int) (hash []byte)

  • Sha3.Shake256(data []byte, size int) (hash []byte)

Options.Mode

block cipher mode

  • MODE_CBC default

    CBC Cipher-block chaining

  • MODE_CFB

    CFB Cipher feedback

  • MODE_CTR

    CTR Counter mode

  • MODE_OFB

    OFB Output feedback

  • MODE_GCM

    GCM Galois/Counter Mod

  • MODE_ECB

    ECB Electronic codebook

Options.Padding

  • PAD_PKCS7 default

    PKCS#7 RFC 5652

  • PAD_ISO97971

    ISO/IEC 9797-1 Padding Method 2

  • PAD_ANSIX923

    ANSI X.923

  • PAD_ISO10126

    ISO 10126

  • PAD_ZEROPADDING

    Zero padding

  • PAD_NOPADDING

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AES cryptAES

Shortcuts

View Source
var Blowfish cryptBlowfish
View Source
var ChaCha20 cryptChaCha20
View Source
var DES cryptDES
View Source
var DES3 tripleDES
View Source
var MD5 cryptMD5
View Source
var RC4 cryptRC4
View Source
var SHA3 cryptSha3

Functions

func AnsiX923Padding

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

ANSI X.923 padding

func AnsiX923UnPadding

func AnsiX923UnPadding(ciphertext []byte, blockSize int) ([]byte, error)

func ISO10126Padding

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

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

func ISO10126UnPadding

func ISO10126UnPadding(ciphertext []byte, blockSize int) ([]byte, error)

func ISO97971Padding

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

ISO/IEC 9797-1 Padding Method 2

func ISO97971UnPadding

func ISO97971UnPadding(ciphertext []byte, blockSize int) ([]byte, error)

func PKCS7Padding

func PKCS7Padding(plaintext []byte, blockSize int) ([]byte, error)

PKCS7

func PKCS7UnPadding

func PKCS7UnPadding(ciphertext []byte, blockSize int) ([]byte, error)

func Padding

func Padding(scheme PaddingScheme, plaintext []byte, blockSize int) (padded []byte, err error)

func UnPadding

func UnPadding(scheme PaddingScheme, ciphertext []byte, blockSize int) (data []byte, err error)

func ZeroPadding

func ZeroPadding(plaintext []byte, blockSize int) ([]byte, error)

Zero padding

func ZeroUnPadding

func ZeroUnPadding(ciphertext []byte, _ int) ([]byte, error)

Types

type BlockMode

type BlockMode uint8
const (
	MODE_CBC BlockMode = iota
	MODE_CFB
	MODE_CTR
	MODE_OFB
	MODE_GCM
	MODE_ECB
)

func (BlockMode) Has

func (mode BlockMode) Has(modes ...BlockMode) bool

func (BlockMode) Not

func (mode BlockMode) Not(modes ...BlockMode) bool

func (BlockMode) String

func (mode BlockMode) String() string

type CipherMethod

type CipherMethod uint8
const (
	METHOD_AES CipherMethod = iota
	METHOD_DES
	METHOD_DES3
	METHOD_CHACHA20
	METHOD_BLOWFISH
	METHOD_RC4
)

func (CipherMethod) String

func (method CipherMethod) String() string

type Crypt

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

func NewAES

func NewAES(key, iv []byte, args ...Options) (*Crypt, error)

func NewBlowfish

func NewBlowfish(key []byte) (*Crypt, error)

func NewChaCha20

func NewChaCha20(key, iv []byte) (*Crypt, error)

func NewDES

func NewDES(key, iv []byte, args ...Options) (*Crypt, error)

func NewDES3

func NewDES3(key, iv []byte, args ...Options) (*Crypt, error)

func NewRC4

func NewRC4(key []byte) (*Crypt, error)

func (Crypt) Decrypt

func (c Crypt) Decrypt(src []byte) ([]byte, error)

func (Crypt) Encrypt

func (c Crypt) Encrypt(src []byte) ([]byte, error)

type Options

type Options struct {
	Mode    BlockMode
	Padding PaddingScheme
}

type PaddingScheme

type PaddingScheme uint8
const (
	PAD_PKCS7 PaddingScheme = iota
	PAD_ISO97971
	PAD_ANSIX923
	PAD_ISO10126
	PAD_ZEROPADDING
	PAD_NOPADDING
)

func (PaddingScheme) String

func (scheme PaddingScheme) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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