Standard library / crypto/rsa

crypto/rsa.GenerateKey()

Generates an RSA private key of the requested modulus size using secure randomness. Use at least 2048 bits and follow protocol requirements. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func GenerateKey(random io.Reader, bits int) (*PrivateKey, error)

Parameters & Attributes

NameTypeDescription
randomio.ReaderCryptographically secure random source; see description for exceptions.
bitsintRequested modulus bit length.

Return Value

(*PrivateKey, error)

Result as described above; check any returned error before use.

Working Example

Example Snippetgo
package main
import (  "crypto/rand"  "crypto/rsa"  "fmt")
func main() {  k := must(rsa.GenerateKey(rand.Reader, 2048))  fmt.Println(k.N.BitLen() == 2048 && k.Validate() == nil)}func must[T any](v T, e error) T {  if e != nil {    panic(e)  }  return v}func check(e error) {  if e != nil {    panic(e)  }}
Go Reference