crypto

package
v0.0.0-...-3db7acb Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2020 License: Apache-2.0 Imports: 13 Imported by: 0

README

crypto

DES,3DES,Aes使用方法:

  • DES,3DES,Aes

package main

import (
	"encoding/base64"
	"fmt"
	"github.com/Eric-GreenComb/contrib/crypto"
)

func main() {
	// DES 加解密
	testDes()
	// 3DES加解密
	test3Des()

	testAes()
}

func testDes() {
	key := []byte("sfe023f_")
	result, err := crypto.DesEncrypt([]byte("polaris@studygolang"), key)
	if err != nil {
		panic(err)
	}
	fmt.Println(base64.StdEncoding.EncodeToString(result))
	origData, err := crypto.DesDecrypt(result, key)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(origData))
}

func test3Des() {
	key := []byte("sfe023f_sefiel#fi32lf3e!")
	result, err := crypto.TripleDesEncrypt([]byte("polaris@studygolang"), key)
	if err != nil {
		panic(err)
	}
	fmt.Println(base64.StdEncoding.EncodeToString(result))
	origData, err := crypto.TripleDesDecrypt(result, key)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(origData))
}

func testAes() {
	// AES-128。key长度:16, 24, 32 bytes 对应 AES-128, AES-192, AES-256
	key := []byte("sfe023f_9fd&fwflsfe023f_9fd&fwfl")
	// key := []byte("sfe023f_9fd&fwfl")
	result, err := crypto.AesEncrypt([]byte("polaris@studygolang"), key)
	if err != nil {
		panic(err)
	}
	fmt.Println(base64.StdEncoding.EncodeToString(result))
	origData, err := crypto.AesDecrypt(result, key)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(origData))
}

RSA使用方法:

  • 生成RSA公私钥文件

package main

import (
	"github.com/banerwai/gommon/crypto"
	"log"
)

func main() {
	var bits int
	bits = 2048
	if err := crypto.RsaGenKey(bits); err != nil {
		log.Fatal("密钥文件生成失败!")
	}
	log.Println("密钥文件生成成功!")
}

  • RsaEncrypt 公钥加密
  • RsaDecrypt 私钥解密

package main

import (
	"fmt"
	"github.com/banerwai/gommon/crypto"
	"io/ioutil"
	"os"
	"time"
)

func main() {

	initData := "abcdefghij_klmnopq_"
	init := []byte(initData)

	data, err := crypto.RsaEncrypt(init, publicKey)
	if err != nil {
		panic(err)
	}
	pre := time.Now()
	origData, err := crypto.RsaDecrypt(data, privateKey)
	if err != nil {
		panic(err)
	}
	now := time.Now()
	fmt.Println(now.Sub(pre))
	fmt.Println(string(origData))

}

var decrypted string
var privateKey, publicKey []byte

func init() {
	var err error
	// flag.StringVar(&decrypted, "d", "", "加密过的数据")
	// flag.Parse()
	publicKey, err = ioutil.ReadFile("public.pem")
	if err != nil {
		os.Exit(-1)
	}
	privateKey, err = ioutil.ReadFile("private.pem")
	if err != nil {
		os.Exit(-1)
	}
}


  • RsaSign,RsaVerify

package main

import (
	"crypto"
	"crypto/md5"
	"fmt"
	banerwaicrypto "github.com/banerwai/gommon/crypto"
	"io/ioutil"
	"os"
)

func main() {

	initData := "abcdefghijklmnopq"
	init := []byte(initData)
	hashed := md5.Sum(init)
	_sign, err := banerwaicrypto.RsaSign(crypto.MD5, hashed[:], privateKey)

	init1 := []byte("abcdefghijklmnopq")
	hashed1 := md5.Sum(init1)

	err = banerwaicrypto.RsaVerify(crypto.MD5, hashed1[:], publicKey, _sign)
	if err != nil {
		fmt.Println(err.Error())
	} else {
		fmt.Println("Sign is right")
	}

}

var decrypted string
var privateKey, publicKey []byte

func init() {
	var err error
	// flag.StringVar(&decrypted, "d", "", "加密过的数据")
	// flag.Parse()
	publicKey, err = ioutil.ReadFile("public.pem")
	if err != nil {
		os.Exit(-1)
	}
	privateKey, err = ioutil.ReadFile("private.pem")
	if err != nil {
		os.Exit(-1)
	}
}


Documentation

Index

Constants

View Source
const (
	// KcRandKindNum 纯数字
	KcRandKindNum = 0
	// KcRandKindLower 小写字母
	KcRandKindLower = 1
	// KcRandKindUpper 大写字母
	KcRandKindUpper = 2
	// KcRandKindAll 数字、大小写字母
	KcRandKindAll = 3
)

Variables

This section is empty.

Functions

func AesDecrypt

func AesDecrypt(crypted, key []byte) ([]byte, error)

AesDecrypt decrypt aes 2 []byte

func AesEncrypt

func AesEncrypt(origData, key []byte) ([]byte, error)

AesEncrypt encrypt aes 2 []byte

func BanerwaiAPIV1CheckSign

func BanerwaiAPIV1CheckSign(sign, apiKey string, args ...string) bool

BanerwaiAPIV1CheckSign banerwai api sign check

func BanerwaiAPIV1GenSign

func BanerwaiAPIV1GenSign(apiKey string, args ...string) string

BanerwaiAPIV1GenSign banerwai api gen sign

func Base64Decode

func Base64Decode(dec []byte) ([]byte, error)

Base64Decode base64 decode 2 []byte

func Base64Encode

func Base64Encode(src []byte) []byte

Base64Encode base64 encode 2 []byte

func CompareDoubleMd5

func CompareDoubleMd5(source, md5 string) bool

CompareDoubleMd5 compare double md5 value

func CompareHS256Hex

func CompareHS256Hex(in, secret, hs256Hex string) bool

CompareHS256Hex compare HS256Hex value

func CompareHS512Hex

func CompareHS512Hex(in, secret, hs512Hex string) bool

CompareHS512Hex compare HS512Hex value

func CompareHash

func CompareHash(digest []byte, password string) bool

CompareHash compares bcrypt password with a plaintext one. Returns true if passwords match and false if they do not.

func CompareMd5

func CompareMd5(source, md5 string) bool

CompareMd5 compare md5 value

func CompareSHA256Hex

func CompareSHA256Hex(in, sha256Hex string) bool

CompareSHA256Hex compare sha256 value

func CompareSHA512Hex

func CompareSHA512Hex(in, sha512Hex string) bool

CompareSHA512Hex compare sha512 value

func DecodeBase64

func DecodeBase64(dec string) (string, error)

DecodeBase64 base64 decode 2 string

func DecryptAes

func DecryptAes(crypted, key []byte) ([]byte, error)

DecryptAes decrypt by aes

func DecryptAesString

func DecryptAesString(crypted, key string) (string, error)

DecryptAesString decrypt aes 2 string

func DesDecrypt

func DesDecrypt(crypted, key []byte) ([]byte, error)

DesDecrypt des decrypt 2 []byte

func DesEncrypt

func DesEncrypt(origData, key []byte) ([]byte, error)

DesEncrypt des encrypt 2 []byte

func DoubleMd5

func DoubleMd5(source string) string

DoubleMd5 double md5 encrypt 2 string

func EncodeBase64

func EncodeBase64(src string) string

EncodeBase64 base64 encode 2 string

func EncryptAes

func EncryptAes(origData, key []byte) ([]byte, error)

EncryptAes encrypt by aes

func EncryptAesString

func EncryptAesString(origData, key string) (string, error)

EncryptAesString encrypt aes 2 string

func GenerateHash

func GenerateHash(password string) ([]byte, error)

GenerateHash generates bcrypt hash from plaintext password

func GetRandomItNum

func GetRandomItNum(num int) int

GetRandomItNum 返回输入参数以下的随机数字,如输入参数为5,随机返回0,1,2,3,4

func GetRandomString

func GetRandomString(num int) string

GetRandomString 生成随机字符串

func HS256Base64

func HS256Base64(in, secret string) string

HS256Base64 hs256 in string/out base64

func HS256Byte

func HS256Byte(in, secret []byte) []byte

HS256Byte hs256 byte in/out

func HS256Hex

func HS256Hex(in, secret string) string

HS256Hex hs256 in string /out hex

func HS512Base64

func HS512Base64(in, secret string) string

HS512Base64 hs512 in string/out base64

func HS512Byte

func HS512Byte(in, secret []byte) []byte

HS512Byte hs512 byte in/out

func HS512Hex

func HS512Hex(in, secret string) string

HS512Hex hs512 in string /out hex

func Krand

func Krand(size int, kind int) string

Krand 随机字符串

func Md5

func Md5(source string) string

Md5 md5 encrypt 2 string

func PKCS5Padding

func PKCS5Padding(ciphertext []byte, blockSize int) []byte

PKCS5Padding des PKCS5Padding

func PKCS5UnPadding

func PKCS5UnPadding(origData []byte) []byte

PKCS5UnPadding des PKCS5UnPadding

func RandomPwd

func RandomPwd(num int) string

RandomPwd 随机密码 num 几位

func SHA256Base64

func SHA256Base64(in string) string

SHA256Base64 sha256 in string/out base64

func SHA256Byte

func SHA256Byte(in []byte) []byte

SHA256Byte sha256 byte in/out

func SHA256Hex

func SHA256Hex(in string) string

SHA256Hex sha256 in string /out hex

func SHA512Base64

func SHA512Base64(in string) string

SHA512Base64 sha512 in string/out base64

func SHA512Byte

func SHA512Byte(in []byte) []byte

SHA512Byte sha512 byte in/out

func SHA512Hex

func SHA512Hex(in string) string

SHA512Hex sha512 in string /out hex

func TripleDesDecrypt

func TripleDesDecrypt(crypted, key []byte) ([]byte, error)

TripleDesDecrypt 3DES解密

func TripleDesEncrypt

func TripleDesEncrypt(origData, key []byte) ([]byte, error)

TripleDesEncrypt 3DES加密

func ZeroPadding

func ZeroPadding(ciphertext []byte, blockSize int) []byte

ZeroPadding des zeropadding

func ZeroUnPadding

func ZeroUnPadding(origData []byte) []byte

ZeroUnPadding des zerounpadding

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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