goencrypt

package module
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2022 License: Apache-2.0 Imports: 22 Imported by: 7

README

goencrypt

Goencrypt is a library written in pure Go providing a set of encryption algorithms, which including symmetric ciphers(DES, 3DES, AES), asymmetric ciphers (RSA), etc.

Quick Start

Download and install

go get github.com/marspere/goencrypt@v1.0.7
# assume the following codes in example.go file
$ cat example.go
package main

import (
	"fmt"

	"github.com/marspere/goencrypt"
)

func main() {
	value, err := goencrypt.MD5("hello world")
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(value)
	// output: 5eb63bbbe01eeed093cb22bb8f5acdc3
}
# run example.go
$ go run example.go

API Examples

You can find a number of examples at goencrypt repository.

MD5 Message-Digest Algorithm

It is a widely used cryptographic hash function that produces a hash value to ensure complete and consistent information transfer.

func main() {	
    // The return value is 32-bit lowercase.
    value, err := goencrypt.MD5("hello world")
    fmt.Println(value.Value)
    
    // UpperCase32 return 32-bit uppercase value.
    fmt.Println(value.UpperCase32)
    
    // LowerCase16 return 16-bit lowercase value.
    fmt.Println(value.LowerCase16)
    
    // UpperCase16 return 16-bit uppercase value.
    fmt.Println(value.UpperCase16)
}

RSA Algorithm

RSA encryption algorithm is an asymmetric encryption algorithm. RSA is also a packet encryption algorithm, except that the packet size can be changed according to the size of the key.

RSA encryption limits the length of plaintext, and specifies the maximum length of plaintext to be encrypted = len(key) - 11.

func main() {
	cipher := goencrypt.NewRSACipher(goencrypt.PrintBase64, defaultPublicFile, defaultPrivateFile)
	cipherText, err := cipher.RSAEncrypt([]byte("hello world"))
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(cipherText)
}

AES Algorithm

AES, Advanced Encryption Standard, also known as Rijndael encryption in cryptography, is a block encryption standard adopted by the US federal government.

AES block length is fixed at 128 bits, the key length can be 128, 192 or 256 bits. It including AES-ECB,AES-CBC,AES-CTR,AES-OFB,AES-CFB.

func main() {
	cipher, err := goencrypt.NewAESCipher([]byte("0123456789asdfgh"), []byte("0123456789asdfgh"), goencrypt.CBCMode, goencrypt.Pkcs7, goencrypt.PrintBase64)
	if err != nil {
    	fmt.Println(err)
    	return
    }
	cipherText, err := cipher.AESEncrypt([]byte("hello world"))
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(cipherText)
}

DES Algorithm
func main() {
	cipher := goencrypt.NewDESCipher([]byte("12345678"), []byte(""), goencrypt.ECBMode, goencrypt.Pkcs5, goencrypt.PrintBase64)
	cipherText, err := cipher.DESEncrypt([]byte("hello world"))
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(cipherText)
}

Triple DES Algorithm
func main() {
	cipher := goencrypt.NewDESCipher([]byte("12345678abcdefghijklmnop"), []byte("abcdefgh"), goencrypt.CBCMode, goencrypt.Pkcs5, goencrypt.PrintBase64)
	cipherText, err := cipher.TripleDESEncrypt([]byte("hello world"))
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(cipherText)
}

Secure Hash Algorithm
func main() {
	result, err := goencrypt.SHA(goencrypt.SHA1, []byte("hello world"), goencrypt.PrintHex)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(result)
}

Contributing

If you’d like to propose a change please ensure the following:

  • All existing tests are passing.
  • There are tests in the test suite that cover the changes you’re making.
  • You have added documentation strings (in English) to (at least) the public functions you’ve added or modified.

Documentation

Index

Examples

Constants

View Source
const (
	CBCMode = iota
	CFBMode
	CTRMode
	ECBMode
	OFBMode
)
View Source
const (
	PrintHex = iota
	PrintBase64
)
View Source
const (
	SHA1 = iota
	SHA256
	SHA512
)
View Source
const BlockSize = 16

Variables

This section is empty.

Functions

func SHA added in v1.0.2

func SHA(length int, content interface{}, decodeType int) (result string, err error)

SHA implements several hash functions, including sha1, sha256, and sha512. Meanwhile, SHA supports file hash. When content type is string, it indicates the address of the file. decodeType represents the print format, and the result is not encrypted by default.

Example
result, err := SHA(SHA1, []byte("hello world"), PrintHex)
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println(result)
Output:

Types

type Cipher added in v1.0.1

type Cipher struct {
	GroupMode  int
	FillMode   FillMode
	DecodeType int
	Key        []byte
	Iv         []byte
	Output     CipherText
}

func (*Cipher) Decode added in v1.0.1

func (c *Cipher) Decode(cipherText string) ([]byte, error)

func (*Cipher) Decrypt added in v1.0.1

func (c *Cipher) Decrypt(block cipher.Block, cipherData []byte) (err error)

func (*Cipher) Encode added in v1.0.1

func (c *Cipher) Encode() string

Encode default print format is base64

func (*Cipher) Encrypt added in v1.0.1

func (c *Cipher) Encrypt(block cipher.Block, plainData []byte) (err error)

func (*Cipher) Fill added in v1.0.1

func (c *Cipher) Fill(plainText []byte, blockSize int) []byte

func (*Cipher) NewECBDecrypter added in v1.0.1

func (c *Cipher) NewECBDecrypter(block cipher.Block, cipherData []byte)

func (*Cipher) NewECBEncrypter added in v1.0.1

func (c *Cipher) NewECBEncrypter(block cipher.Block, plainData []byte)

func (*Cipher) UnFill added in v1.0.1

func (c *Cipher) UnFill(plainText []byte) (data []byte, err error)

type CipherAES added in v1.0.1

type CipherAES struct {
	Cipher
}

func NewAESCipher added in v1.0.1

func NewAESCipher(key, iv []byte, groupMode int, fillMode FillMode, decodeType int) (*CipherAES, error)

func (*CipherAES) AESDecrypt added in v1.0.1

func (c *CipherAES) AESDecrypt(cipherText string) (plainText string, err error)

func (*CipherAES) AESEncrypt added in v1.0.1

func (c *CipherAES) AESEncrypt(plainText []byte) (cipherText string, err error)

type CipherDES added in v1.0.1

type CipherDES struct {
	Cipher
}

func NewDESCipher added in v1.0.1

func NewDESCipher(key, iv []byte, groupMode int, fillMode FillMode, decodeType int) *CipherDES

func (*CipherDES) DESDecrypt added in v1.0.1

func (c *CipherDES) DESDecrypt(cipherText string) (plainText string, err error)

func (*CipherDES) DESEncrypt added in v1.0.1

func (c *CipherDES) DESEncrypt(plainText []byte) (cipherText string, err error)

func (*CipherDES) TripleDESDecrypt added in v1.0.1

func (c *CipherDES) TripleDESDecrypt(cipherText string) (plainText string, err error)

func (*CipherDES) TripleDESEncrypt added in v1.0.1

func (c *CipherDES) TripleDESEncrypt(plainText []byte) (cipherText string, err error)

type CipherRSA added in v1.0.1

type CipherRSA struct {
	PubKey string
	PriKey string
	Cipher
}

func NewRSACipher added in v1.0.1

func NewRSACipher(decodeType int, keyFile ...string) *CipherRSA

New returns a CipherRSA pointer, the keyFile parameters is address of public key or private key. If the length of keyFile is 1, which means RSA encryption. If the length of keyFile greater than 1, which means RSA decryption. Meanwhile, the second element of keyFile represents private key.

func (*CipherRSA) RSADecrypt added in v1.0.1

func (cr *CipherRSA) RSADecrypt(cipherText string) (plainText string, err error)

Decrypt parses the given message with RSA private key in PKCS#1, ASN.1 DER form.

func (*CipherRSA) RSAEncrypt added in v1.0.1

func (cr *CipherRSA) RSAEncrypt(plainText []byte) (cipherText string, err error)

Encrypt encrypts the given message with RSA and the padding scheme from PKCS#1 v1.5.

type CipherText added in v1.0.1

type CipherText []byte

type Crypto added in v1.0.1

type Crypto interface {
	Encrypt(plainText []byte) (string, error)
	Decrypt(cipherText string) (string, error)
}

type FillMode added in v1.0.1

type FillMode int
const (
	PkcsZero FillMode = iota
	Pkcs7
)

type MessageDigest added in v1.0.1

type MessageDigest string

func MD5

func MD5(src interface{}) (md MessageDigest, err error)

MD5 information summary, the src has two types: []byte or string The default return value is 32-bit lowercase.

func (MessageDigest) LowerCase16 added in v1.0.1

func (md MessageDigest) LowerCase16() string

func (MessageDigest) UpperCase16 added in v1.0.1

func (md MessageDigest) UpperCase16() string

func (MessageDigest) UpperCase32 added in v1.0.1

func (md MessageDigest) UpperCase32() string

Jump to

Keyboard shortcuts

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