Giter VIP home page Giter VIP logo

secret's Introduction

usual encryption algorithm written in go

Function

Cipher
Type Mode Padding
DES ECB/CBC PKCS5/PKCS7/Zero/None
3DES ECB/CBC PKCS5/PKCS7/Zero/None
AES ECB/CBC PKCS5/PKCS7/Zero/None
DES CFB/OFB/CTR/GCM
3DES CFB/OFB/CTR/GCM
AES CFB/OFB/CTR/GCM
RC4 - -
RSA Encrypt/Decrypt/Sign/Verify -
Hash
Name
Hash
DoubleHash
Sign&&Verify
Name Description
ECC(ECDSA) Sign/Verify
DSA Sign/Verify
EdDSA Sign/Verify

Example

package main

import (
	"crypto/rand"
	"fmt"
	"github.com/pyihe/secret"
	"io"
)

func main() {
    var c = secret.NewCipher()
    var nonce = make([]byte, 12) // 随机向量
    _, _ = io.ReadFull(rand.Reader, nonce)
    //encrypt
    var encryptReq = &secret.SymRequest{
        PlainData:   "I Love China!",
        Key:         []byte("1234567812345678"),
        Type:        secret.SymTypeAES,
        ModeType:    secret.BlockModeGCM,
        PaddingType: secret.PaddingTypeNoPadding,
        AddData:     []byte("this is additional data"),
        Nonce:       nonce,
    }
    cipherStr, err := c.SymEncryptToString(encryptReq)
    if err != nil {
        //handle err
    }
    
    //decrypt
    //if decrypts in another server with GCM mode, then your need to set nonce to decrypt
    var decryptReq = &secret.SymRequest{
        PlainData:   nil,
        CipherData:  cipherStr,
        Key:         []byte("1234567812345678"),
        Type:        secret.SymTypeAES,
        ModeType:    secret.BlockModeGCM,
        PaddingType: secret.PaddingTypeNoPadding,
        AddData:     []byte("this is additional data"),
        Nonce:       nonce, // this value must be the same with encryptReq
    }
    plainBytes, err := c.SymDecrypt(decryptReq)
    if err != nil {
        //handle err
    }
    //unmarshal plainBytes or use it directly
    
    //c.SetRSAKey(privateKey, secret.PKCSLevel1) to set key if key is already exist
    _, _, err = c.GenerateRSAKey(4096, "../conf", secret.PKCSLevel1)
    if err != nil {
        //handle err
    }
    
    cipherStr, err = c.RSAEncryptToString(plainBytes, secret.RSAEncryptTypeOAEP, nil)
    if err != nil {
        //handle err
    }
    plainBytes, err = c.RSADecrypt(cipherStr, secret.RSAEncryptTypeOAEP, nil)
    if err != nil {
        //handle err
    }
    //output: I Love China!
    fmt.Printf("%s\n", plainBytes)
}

secret's People

Contributors

pyihe avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

icyxp

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.