Golang1.7.3简单的rsa加密封装

Golang1.7.3简单的rsa加密封装,第1张

概述package rsasimport ( "bytes" "crypto/rand" "crypto/rsa" "crypto/x509" "crypto/x509/pkix" "encoding/pem" "errors" "io" "io/ioutil" "math/big" rd "math/rand
package rsasimport (    "bytes"    "crypto/rand"    "crypto/rsa"    "crypto/x509"    "crypto/x509/pkix"    "enCoding/pem"    "errors"    "io"    "io/IoUtil"    "math/big"    rd "math/rand"    "net"    "os"    "path/filepath"    "time")//var Certificate = struct {// RootCA *x509.Certificate// RootKey//}{}//func InitRootCA(crt,key string) (err error) {// Certificate.RootCA,Certificate.RootKey,err = Parse(crt,key)// return//}/* x := rsas.Certinformation{ Country: []string{"CH"},Organization: []string{"www.work-stacks.com"},OrganizationalUnit: []string{"Paas"},EmailAddress: []string{"czxichen@163.com"},Province: []string{"Js"},Locality: []string{"SZ"},Commonname: "master.work-stacks.com",DNSnames: []string{"master.work-stacks.com"},EncryptLen: 512,IsCA: true,DateLen: 5,} */func init() {    rd.Seed(time.Now().UnixNano())}type Certinformation struct {    Country            []string    Organization       []string    OrganizationalUnit []string //使用者    EmailAddress       []string    Province           []string //省    Locality           []string //市    Commonname         string   //域名    DNSnames           []string    IPAddresses        []net.IP    IsCA               bool //是否是根证书    names              []pkix.AttributeTypeAndValue    Crtname,Keyname   string    EncryptLen         int //密钥长度    DateLen            int //有效期,单位年}func SignerCRT(rootcrt *x509.Certificate,rootkey *rsa.PrivateKey,crt *x509.Certificate) ([]byte,error) {    if rootcrt == nil || rootkey == nil {        return nil,errors.New("Root crt is null")    }    buf,err := x509.CreateCertificate(rand.Reader,crt,rootcrt,crt.PublicKey,rootkey)    b := bytes.NewBuffer(nil)    err = Write(b,buf,"CERTIFICATE")    return b.Bytes(),err}func CheckSignature(rootcrt *x509.Certificate,crt []byte) error {    ca,err := ParseCrt(crt)    if err != nil {        return err    }    return ca.CheckSignatureFrom(rootcrt)}func CreatePemCRT(info Certinformation) (pemcrt []byte,pemkey []byte,err error) {    pemcrt,pemkey,err = CreateCRT(nil,nil,info)    if err != nil {        return    }    cfile := bytes.NewBuffer([]byte{})    err = Write(cfile,pemcrt,"CERTIFICATE")    if err != nil {        return    }    pemcrt = cfile.Bytes()    kfile := bytes.NewBuffer([]byte{})    err = Write(kfile,"PRIVATE KEY")    pemkey = kfile.Bytes()    return}func CreateCRT(RootCa *x509.Certificate,RootKey *rsa.PrivateKey,info Certinformation) (crt []byte,key []byte,err error) {    Crt := newCertificate(info)    if info.EncryptLen < 512 {        info.EncryptLen = 512    }    Key,err := rsa.GenerateKey(rand.Reader,info.EncryptLen)    if err != nil {        return    }    key = x509.MarshalPKCS1PrivateKey(Key)    if RootCa == nil || RootKey == nil {        crt,err = x509.CreateCertificate(rand.Reader,Crt,&Key.PublicKey,Key)    } else {        crt,RootCa,RootKey)    }    return}func Wirtefile(path string,buf []byte,typ string) error {    os.MkdirAll(filepath.Dir(path), 0666)    file,err := os.Create(path)    defer file.Close()    if err != nil {        return err    }    return Write(file,typ)}func Write(w io.Writer,typ string) error {    b := &pem.Block{Bytes: buf,Type: typ}    return pem.Encode(w,b)}func Parse(crtPath,keyPath string) (rootcertificate *x509.Certificate,rootPrivateKey *rsa.PrivateKey,err error) {    buf,err := IoUtil.Readfile(crtPath)    if err != nil {        return    }    rootcertificate,err = ParseCrt(buf)    if err != nil {        return    }    buf,err = IoUtil.Readfile(keyPath)    if err != nil {        return    }    rootPrivateKey,err = ParseKey(buf)    return}func ParseCrt(buf []byte) (*x509.Certificate,error) {    p := &pem.Block{}    p,_ = pem.Decode(buf)    return x509.ParseCertificate(p.Bytes)}func ParseKey(buf []byte) (*rsa.PrivateKey,error) {    p,buf := pem.Decode(buf)    return x509.ParsePKCS1PrivateKey(p.Bytes)}func newCertificate(info Certinformation) *x509.Certificate {    if info.DateLen == 0 {        info.DateLen = 10    }    return &x509.Certificate{        SerialNumber: big.NewInt(rd.Int63()),Subject: pkix.name{            Country:            info.Country,Organization:       info.Organization,OrganizationalUnit: info.OrganizationalUnit,Province:           info.Province,Commonname:         info.Commonname,Locality:           info.Locality,Extranames:         info.names,},NotBefore:             time.Now(),NotAfter:              time.Now().AddDate(info.DateLen, 0, 0),BasicConstraintsValID: true,DNSnames:              info.DNSnames,IPAddresses:           info.IPAddresses,IsCA:                  info.IsCA,ExtKeyUsage:           []x509.ExtKeyUsage{x509.ExtKeyUsageClIEntAuth,x509.ExtKeyUsageServerAuth},KeyUsage:              x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,EmailAddresses:        info.EmailAddress,}}
总结

以上是内存溢出为你收集整理的Golang1.7.3简单的rsa加密封装全部内容,希望文章能够帮你解决Golang1.7.3简单的rsa加密封装所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1280867.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存