Giter Site home page Giter Site logo

forkgitss / marspere_goencrypt Goto Github PK

View Code? Open in Web Editor NEW

This project forked from marspere/goencrypt

0.0 0.0 0.0 139 KB

Go实现的各种加密算法,包括对称密码(DES,3DES,AES),非对称密码(DH,RSA)等

License: Apache License 2.0

Go 100.00%

marspere_goencrypt's Introduction

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/[email protected]
# 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.

marspere_goencrypt's People

Contributors

marspere avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.