Help us shape the future of Go! Take the 2026 Go Developer Survey

securerandom

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 9, 2016 License: BSD-3-Clause Imports: 3 Imported by: 12

README

go-securerandom

License

Package securerandom is a set of utilities for generating random data from the crypto/rand package. The package has the ability to generate bytes from secure-random data, as well as other data from those bytes. That includes numbers and Base64-encoded strings.

License

go-securerandom is released under the BSD 3-Clause License. See the LICENSE file for the full contents of the license.

Installation

go get -u github.com/theckman/go-securerandom

Usage

Full usage information can be found on the GoDoc page, but here is a short example:

import (
	"fmt"
	"math/rand"
	"github.com/theckman/go-securerandom"
)

ri64, err := securerandom.GenerateRandomInt64()

// secure-random data is unavailable
if err != nil {
	// handle err
}

rand.Seed(ri64)

You can also generate random base64 encoded strings from this package:

// generate string from 32 random bytes
rStr, err := securerandom.Base64OfBytes(32)

// secure-random data is unavailable
if err != nil { /* handle err */ }

fmt.Println(rStr)

// assume your random string can only be 32 bytes long
rStr, _ = securerandom.Base64InBytes(32)

fmt.Println(rStr) // would print Base64 string with a length of 32

Documentation

Overview

Package securerandom is a set of utilities for generating random data from the secure sources provided by the "crypto/rand" package. The package has the ability to generate bytes from securerandom data, as well as other data from those bytes. That includes numbers and Base64-encoded strings.

Example:

import (
	"math/rand"
	"github.com/theckman/go-securerandom"
)

ri64, err := securerandom.Int64()

// secure-random data is unavailable
if err != nil { /* handle err */ }

rand.Seed(ri64)

You can also generate random base64 encoded strings from this package:

// generate string from 32 random bytes
rStr, err := securerandom.Base64OfBytes(32)

// secure-random data is unavailable
if err != nil { /* handle err */ }

fmt.Println(rStr)

// assume your random string can only be 32 bytes long
rStr, _ = securerandom.Base64InBytes(32)

fmt.Println(rStr) // would print Base64 string with a length of 32

Index

Constants

View Source
const PackageVersion = "0.1.1"

PackageVersion is the semantic version number of this package.

Variables

This section is empty.

Functions

func Base64InBytes

func Base64InBytes(max int) (string, error)

Base64InBytes is a function that returns a randomized standard Base64 string. This does not use the URL encoding. It takes a single parameter that is the maximum possible length of the string, it will get as close as possible.

func Base64OfBytes

func Base64OfBytes(n int) (string, error)

Base64OfBytes is a function that returns a randomized standard Base64 string. This does not use the URL encoding. It takes a single parameter that is the number of bytes to used to generate the value.

func Bytes

func Bytes(n int) ([]byte, error)

Bytes is a function that takes an integer and returns a slice of that length containing random bytes.

func Int16

func Int16() (int16, error)

Int16 is a function that returns a int16 generated by 'bitwise-or'ing 4 bytes from crypto/rand.Read().

func Int32

func Int32() (int32, error)

Int32 is a function that returns a int32 generated by 'bitwise-or'ing 4 bytes from crypto/rand.Read().

func Int64

func Int64() (int64, error)

Int64 is a function that returns a int64 generated by 'bitwise-or'ing 4 bytes from crypto/rand.Read().

func RandSource

func RandSource() (rand.Source, error)

RandSource is a function that returns a Source from the "math/rand" package to be used to create a new pseudorandom generator. If this returns err != nil the value of the source is not suitable for use.

func URLBase64InBytes

func URLBase64InBytes(max int) (string, error)

URLBase64InBytes is a function that returns a random URL encoded Base64 string. This does not use the URL encoding. It takes a single parameter that is the maximum possible length of the string, it will get as close as possible.

func URLBase64OfBytes

func URLBase64OfBytes(n int) (string, error)

URLBase64OfBytes is a function that returns a random URL encoded Base64 string. This is using URL encoding. It takes a single parameter that is the number of bytes to use to generate the value.

func Uint16

func Uint16() (uint16, error)

Uint16 is a function that returns a uint16 generated by 'bitwise-or'ing 4 bytes from crypto/rand.Read().

func Uint32

func Uint32() (uint32, error)

Uint32 is a function that returns a uint32 generated by 'bitwise-or'ing 4 bytes from crypto/rand.Read().

func Uint64

func Uint64() (uint64, error)

Uint64 is a function that returns a uint64 generated by 'bitwise-or'ing 4 bytes from crypto/rand.Read().

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL