Skip to content
Node: crypto.randomBytes(n)github.com/sahilkhaire/gox/crypto

RandomBytes

Overview

RandomBytes returns n cryptographically secure random bytes (crypto.randomBytes).

If you are coming from Node.js, the closest pattern is crypto.randomBytes(n).

Signature

go
func RandomBytes(n int) ([]byte, error)

Compare: Node.js · Standard Go · gox

js
crypto.randomBytes(n)
go
b := make([]byte, n)
_, err := rand.Read(b)
go
import "github.com/sahilkhaire/gox/crypto"

crypto.RandomBytes(n)

Example

go
import "github.com/sahilkhaire/gox/crypto"

crypto.RandomBytes(n)

Tips

Import github.com/sahilkhaire/gox/crypto and call RandomBytes directly. See the comparison below for the standard library equivalent.

Standard library alternative

Use the standard library directly:

go
b := make([]byte, n)
_, err := rand.Read(b)

Back to crypto package overview

MIT Licensed · Built for Node.js developers moving to Go