Standard library / crypto/rand

crypto/rand.Prime()

Generate a probable prime of the requested bit length with a secure source. Bit lengths below 2 return an error. This small prime is only for demonstrating the API, never a production key. Expected stdout: '64 true\n'.

Formal Syntax & Structure

go syntaxgo
func Prime(rand io.Reader, bits int) (*big.Int, error)

Parameters & Attributes

NameTypeDescription
randio.ReaderCryptographically secure random source.
bitsintRequired prime bit length (at least 2).

Return Value

(*big.Int, error)

Probable prime or error.

Working Example

Example Snippetgo
package main
import (  "crypto/rand"  "fmt")
func main() {  n, e := rand.Prime(rand.Reader, 64)  if e != nil {    panic(e)  }  fmt.Println(n.BitLen(), n.ProbablyPrime(20))}
Go Reference