60

golang中crypto/rand包

 5 years ago
source link: https://studygolang.com/articles/16795?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

rand包实现了用于加解密的更安全的随机数生成器。

变量:

var Reader io.Reader

Reader是一个全局、共享的密码用强随机生成器。在Unix类型系统中,会从/dev/urandom读取;而windows中会调用CryptGenRandom API。

func INt(rand io.Reader, max *big.Int) (n *big.Int, err error)

返回一个在[0, max)区间服从均匀分布的随机值,如果max <= 0则会panic。

func Prime(rand io.Reader, nit int) (p *big.Int, err error)

返回一个具体有指定指定字位数的数字,该数字具有很高可能是质素。如果从rand读取时出错,或者bits < 2 会返回错误。

func Read(b []byte) (n int, err error)

本函数时一个使用io.ReadFull调用Reader.Read的辅助性函数。当且仅当err == nil时,返回n==len(b)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK