Need to generate a set of random bytes?

package main

import (
    "crypto/rand"
    "fmt"
)

func GenRandomBytes(size int) (blk []byte, err error) {
    blk = make([]byte, size)
    _, err = rand.Read(blk)
    return
}

func main() {
    if blk, err := GenRandomBytes(32); err != nil {
        panic(err)
    } else {
        sblk := string(blk)
        fmt.Printf("sblk: %s\n", sblk)
        fmt.Printf("hex : %x\n", blk)
    }
}

Categories: Code

Yu

Ideals are like the stars: we never reach them, but like the mariners of the sea, we chart our course by them.

Leave a Reply

Your email address will not be published. Required fields are marked *