在Go中生成随机的固定长度字节数组

问题描述 投票:11回答:3

我有一个字节数组,固定长度为4。

token := make([]byte, 4)

我需要将每个字节设置为一个随机字节。我怎么能这样做,在最有效的事情?就我而言,math/rand方法不提供随机字节功能。

也许有内置的方式,或者我应该生成随机字符串并将其转换为字节数组?

arrays go random slice
3个回答
22
投票

Package rand

import "math/rand" 

func Read

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

Read从默认Source生成len(p)个随机字节,并将它们写入p。它总是返回len(p)和nil错误。

func (*Rand) Read

func (r *Rand) Read(p []byte) (n int, err error)

Read生成len(p)个随机字节并将它们写入p。它总是返回len(p)和nil错误。

例如,

package main

import (
    "math/rand"
    "fmt"
)

func main() {
    token := make([]byte, 4)
    rand.Read(token)
    fmt.Println(token)
}

输出:

[187 163 35 30]

10
投票

Go 1.6为math/rand包添加了一个新功能:

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

用随机数据填充传递的byte切片。使用这个rand.Read()

token := make([]byte, 4)
if _, err := rand.Read(token); err != nil {
    // Handle err
}
fmt.Println(token)

rand.Read()有2个返回值:“读取”字节数和(可选)error。这是为了符合一般的io.Reader接口,但rand.Read()的文档声明(尽管它的签名)它永远不会实际返回非nil错误,所以我们可能省略检查它,这简化了它:

token := make([]byte, 4)
rand.Read(token){
fmt.Println(token)

在使用rand.Seed()包之前,不要忘记调用math/rand来正确初始化它,例如:

rand.Seed(time.Now().UnixNano())

注意:在Go 1.6之前没有math/rand.Read()函数,但有(并且仍然是)crypto/rand.Read()函数,但crypto/rand包实现了加密安全的伪随机数生成器,因此它比math/rand慢得多。


2
投票

使用math.Rand意味着您正在使用操作系统提供的系统CSPRNG。这意味着使用/ dev / urandom /和Windows'CryptGenRandom API。谢天谢地,Go的加密/ rand包将这些实现细节抽象出去,以尽量减少错误的风险。

import(
   "crypto/rand"
   "encoding/base64"
 )

// GenerateRandomBytes returns securely generated random bytes. 
// It will return an error if the system's secure random
// number generator fails to function correctly, in which
// case the caller should not continue.
func GenerateRandomBytes(n int) ([]byte, error) {
     b := make([]byte, n)
    _, err := rand.Read(b)
    // Note that err == nil only if we read len(b) bytes.
    if err != nil {
       return nil, err
   }

   return b, nil
}
© www.soinside.com 2019 - 2024. All rights reserved.