Giter VIP home page Giter VIP logo

crypto's Introduction

Golang的加解密、签验名的二次封装库

一、介绍

!!😫 。😘😘

二、安装

go get -u github.com/ggdream/crypto

三、例子

package main

import (
	"fmt"
	"github.com/ggdream/crypto/rsa"
)


func main() {
	pri, pub := rsa.GenerateKey(1024)
    
	cipher := rsa.Encrypt([]byte("我喜欢你"), pub)
	fmt.Println(cipher)
    
	plaint := rsa.Decrypt(cipher, pri)
	fmt.Println(string(plaint))
}

四、函数签名

1. RSA

// package: github.com/ggdream/crypto/rsa


// 操作秘钥
func GenerateKey(bits int) (*rsa.PrivateKey, *rsa.PublicKey)
func GenerateHexKey(bits int) (string, string)

func GetEN(publicKey *rsa.PublicKey) (string, string)
func SetEN(exp, mod string) *rsa.PublicKey

func GetPrivateKey(hexKey string) *rsa.PrivateKey
func GetPublicKey(hexKey string) *rsa.PublicKey

func SetPrivateKeyToPem(privateKey *rsa.PrivateKey) string
func SetPublicKeyToPem(publicKey *rsa.PublicKey) string
func GetPrivateKeyFromPem(privateKeyPem string) *rsa.PrivateKey
func GetPublicKeyFromPem(publicKeyPem string) *rsa.PublicKey


// 加密解密
func Encrypt(text []byte, publicKey *rsa.PublicKey) []byte
func Decrypt(text []byte, privateKey *rsa.PrivateKey) []byte

// 签名验签
func Sign(text []byte, privateKey *rsa.PrivateKey) ([]byte, error)
func Verify(text, sign []byte, publicKey *rsa.PublicKey) bool

2. ECC

// package: github.com/ggdream/crypto/ecc


// 操作秘钥
func GenerateKey(model int) (*ecdsa.PrivateKey, *ecdsa.PublicKey)
func GenerateKeyEth() (*ecies.PrivateKey, *ecies.PublicKey)

func SetPrivateKeyToPem(privateKey *ecdsa.PrivateKey) string
func SetPublicKeyToPem(publicKey *ecdsa.PublicKey) string
func GetPrivateKeyFromPem(privateKeyPem string) *ecdsa.PrivateKey
func GetPublicKeyFromPem(publicKeyPem string) *ecdsa.PublicKey


// 加密解密
func Encrypt(text []byte, publicKey  *ecies.PublicKey) []byte
func Decrypt(text []byte, privateKey *ecies.PrivateKey) []byte

// 签名验签
func Sign(text []byte, privateKey *ecdsa.PrivateKey) ([]byte, []byte)
func Verify(text, rBytes, sBytes []byte, publicKey *ecdsa.PublicKey) bool

3. AES

// package: github.com/ggdream/crypto/aes


// 填充方式(AES并没有64位的块, 如果采用PKCS5, 那么实质上就是采用PKCS7。所以我封装的时候直接让PKCS7函数等上PKCS5函数)
func ZeroPadding(text []byte, blockSize int) []byte
func ZeroUnPadding(text []byte) []byte

func PKCS5Padding(text []byte, blockSize int) []byte
func PKCS5UnPadding(text []byte) []byte

var PKCS7Padding func(text []byte, blockSize int) []byte = PKCS5Padding
var PKCS7UnPadding func(text []byte) []byte = PKCS5UnPadding


// 加密解密(CBC模式。里面使用的填充方式是PKCS5(同时也可以被看做使用的是PKCS7))
func Encrypt(text, key, iv []byte) []byte
func Decrypt(text, key, iv []byte) []byte

4. KEY

// package: github.com/ggdream/crypto/key


// 依于MD5生成秘钥
func Gen16ByMD5() []byte

// 依于随机数生成秘钥
func GenByRand(bits int) []byte
func Gen16ByRand() []byte
func Gen32ByRand() []byte
func Gen64ByRand() []byte

5. HASH

// file: github.com/ggdream/crypto/hashers.go
const (
	DefineMD5			= "md5"
	DefineSHA1			= "sha1"
	DefineSHA224		= "sha224"
	DefineSHA256		= "sha256"
	DefineSHA384		= "sha384"
	DefineSHA512		= "sha512"
	DefineSHA3224		= "sha3-224"
	DefineSHA3256		= "sha3-256"
	DefineSHA3384		= "sha3-384"
	DefineSHA3512		= "sha3-512"
)

type Hasher struct
func Hash(algorithm string, data []byte) (string, error)
func (h *Hasher) GetHasher() hash.Hash
func (h *Hasher) ToBytes() ([]byte, error)
func (h *Hasher) ToBase64() (string, error)
func (h *Hasher) ToHex() (string, error)
func FileMD5(file io.Reader) string

6. encode/decode

// file: github.com/ggdream/crypto/coders.go


// Base64编解码
func EnBase64(src []byte) string
func DeBase64(src string) []byte

// 16进制编解码
func EnHex(src []byte) string
func DeHex(src string) []byte

7. byte[]/string

// file: github.com/ggdream/crypto/ptr.go
// to change struct by using ptr.


// byte[]转string
func SliceToString(data []byte) string

// string转byte[]
func StringToSlice(data string) []byte

crypto's People

Contributors

ggdream avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

wahello

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.