Golang crypto/rand.Prime() function example

package crypto/rand

Prime returns a number, p, of the given size, such that p is prime with high probability. Prime will return error for any error returned by rand.Read(1st parameter) or if bits(2nd param) < 2.

Golang crypto/rand.Prime() function usage example

 package main

 import (
 "crypto/rand"
 "fmt"
 "math/big"
 )

 func main() {
 var p *big.Int
 var bits int
 var err error

 bits = 99

 p, err = rand.Prime(rand.Reader, bits)

 if err != nil {
 fmt.Println(err)
 }

 fmt.Printf("%d\n", p)

 }

Sample output :

go run cryptorandprime.go

565350588010525112928194028091

Reference :

http://golang.org/pkg/crypto/rand/#Prime

Advertisement