Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate key pairs in golang

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to generate key pairs in golang. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Use the official package of golang to generate key pairs.

Method for generating key pairs:

/ / generateRsaKey key

Func generateRsaKey (bits int) (err error) {

Var (

File * os.File

PriKey * rsa.PrivateKey

PubKey * rsa.PublicKey

)

/ / obtain the private key

If priKey, err = rsa.GenerateKey (rand.Reader, bits); err! = nil {

Return

}

/ / convert the private key to ASN.1 DER encoding.

DerStream: = x509.MarshalPKCS1PrivateKey (priKey)

/ / Private key parameter

Block: = & pem.Block {

Type: "privateKey"

Headers: map [string] string {"test": "testKey"}

Bytes: derStream

}

/ / create a private.key file

If file, err = os.Create ("private.key"); err! = nil {

Return

}

/ / write data in the file

If err = pem.Encode (file, block); err! = nil {

Return

}

/ / obtain the public key

PubKey = & priKey.PublicKey

If _ derPkix, _ err: = x509.MarshalPKIXPublicKey (pubKey); _ err! = nil {

Err = _ err

Return

} else {

Block = & pem.Block {

Type: "publicKey"

Headers: map [string] string {"test": "testKey"}

Bytes: _ derPkix

}

}

/ / create a public key file

If file, err = os.Create ("public.key"); err! = nil {

Return

}

/ / write data

If err = pem.Encode (file, block); err! = nil {

Return

}

Return

}

Parse the private key to get its length:

/ / getPrivateKeyLen gets the length of the private key

Func getPrivateKeyLen (private [] byte) (ret int, err error) {

Block, _: = pem.Decode (private)

Var (

Data * rsa.PrivateKey

)

If data, err = x509.ParsePKCS1PrivateKey (block.Bytes); err! = nil {

Return

}

Ret = data.N.BitLen ()

Return

}

Parse the public key acquisition length:

/ / getPublicKeyLen gets the length of the public key

Func getPublicKeyLen (public [] byte) (ret int, err error) {

Var (

Block * pem.Block

Data interface {}

)

/ / Decoding

Block, _ = pem.Decode (public)

/ / parse the data

If data, err = x509.ParsePKIXPublicKey (block.Bytes); err! = nil {

Return

}

/ / analyze the data

Switch v: = data. (type) {

Case * rsa.PublicKey:

Ret = v.N.BitLen ()

Default:

Err = errors.New ("data type is not rsa.PublicKey")

}

Return

}

Main function:

Imported package:

Import (

"crypto/rand"

"crypto/rsa"

"crypto/x509"

"encoding/pem"

"errors"

"flag"

"fmt"

"io/ioutil"

"os"

)

Func main () {

Var (

Bits int

Err error

PubKey, priKey [] byte

N int

)

Flag.IntVar (& bits, "b", 1024, "key length, default is 1024 bits")

Flag.Parse ()

/ / generate a key pair

If err = generateRsaKey (bits); err! = nil {

Panic (err)

}

If priKey, err = ioutil.ReadFile ("private.pem"); err! = nil {

Return

}

If pubKey, err = ioutil.ReadFile ("public.pem"); err! = nil {

Return

}

/ / get the length

If n, err = getPrivateKeyLen (priKey); err = nil {

Fmt.Printf ("private key len is% d\ n", n)

} else {

Panic (err)

}

/ / get the length

If n, err = getPublicKeyLen (pubKey); err = nil {

Fmt.Printf ("public key len is% d\ n", n)

} else {

Panic (err)

}

} on how to generate key pairs in golang to share here, I hope the above content can be of some help to you, you can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report