Giter VIP home page Giter VIP logo

go-rsa's Introduction

go-rsa

安装

go get github.com/melf-xyzh/go-rsa

使用方法

sha1withrsa

签名
// 需要签名的文本
context := "helloworld" 
// 签名
sign, err := sha1withrsa.Sign(privateKey, context)
if err != nil {
	log.Fatalln(err)
}
fmt.Println(sign)
验签
// 验签
verify, err := sha1withrsa.RSAVerify(publicKey, context, sign)
if err != nil {
	return
}
fmt.Println(verify)

加密 / 解密

// 加密
encrypt, err := myrsa.RsaEncrypt(publicKey, content)
if err != nil {
	return
}
// 解密
data, err := myrsa.RsaDecrypt(privateKey,encrypt)
if err != nil {
	return
}

生成读取公私钥文件(pem格式 / PKCS1)

// 生成公钥文件(pem格式)
err = myrsa.CreatePrivatePem(privateKey, "")
if err != nil {
	log.Fatal(err)
}
// 读取私钥文件(pem格式)
err = myrsa.CreatePublicPem(publicKey, "")
if err != nil {
	log.Fatal(err)
}
// 读取私钥文件(pem格式)
privateKey, err = myrsa.ReadPrivatePem("cert/private.pem")
if err != nil {
    log.Fatal(err)
}
// 读取公钥文件(pem格式)
publicKey, err = myrsa.ReadPublicPem("cert/public.pem")
if err != nil {
    log.Fatal(err)
}

生成读取公私钥文件(ec格式 / PKCS8)

// 生成私钥文件(ec格式)
err = myrsa.CreatePrivateEC(privateKey, "cert/private.key")
if err != nil {
	log.Fatal(err)
}
// 生成公钥文件(ec格式)
err = myrsa.CreatePublicEC(publicKey, "cert/public.key")
if err != nil {
	log.Fatal(err)
}
// 读取私钥文件(ec格式)
privateKey, err = myrsa.ReadPrivateEC("cert/private.key")
if err != nil {
	log.Fatal(err)
}
// 读取公钥文件(ec格式)
publicKey, err = myrsa.ReadPublicEC("cert/public.key")
if err != nil {
	log.Fatal(err)
}

生成证书文件(CA)

// 生成CA证书
err = myrsa.CreateCertificate(publicKey, privateKey, "cert/ca/")
if err != nil {
	log.Fatal(err)
}
// 导入CA证书
certificate, err := myrsa.LoadCertificate("cert/ca/ca.crt")
if err != nil {
    log.Fatal(err)
}

PFX证书

// 从PFX证书中解析公私钥
privateKey, publicKey, err := pfx.GetPublicAndPrivateKeyFromPfx("zhengshu.pfx", "123456")
if err != nil {
	return
}
pfx.PrintPublicKey(publicKey)
pfx.PrintPrivateKey(privateKey)

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.