Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to generate key pairs in golang

Shulou Source: shulou.com Published: 2022-06-02 07:44:59 09月28日 Update

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.

Tags: Key length generation public key data file content article more knowledge article yes function parameter official form method coding quality analysis Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft MySQL macOS MariaDB Redmi