cryptorand

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2022 License: MIT Imports: 3 Imported by: 2

README

This package brought to you by Adventure Scientists. Read more about our open source policy here.

cryptorand

cryptorand is a simple, zero-dependency Go library that implements the math/rand package's rand.Source and rand.Source64 interfaces backed by the crypto/rand package for cryptographically secure number generation.

This is useful for cases where you don't want a pseudorandom/insecure random number generator, but would like to use the higher-level interface provided by *rand.Rand

Usage

package main

import "github.com/Silicon-Ally/cryptorand"

func main() {
  r := cryptorand.New()
  // Will print a number [0,9]
  fmt.Println(r.Intn(10))
}

Panicking Behavior

Generally, Go libraries should avoid panicking except in extreme circumstances, opting instead to return an error. *cryptorand.Source may panic if the underlying call to crypto/rand.Read fails, which can happen for different reasons on different platforms. Because the math/rand.Source interface doesn't expose an error in the response, we opt to panic loudly instead of silently failing, as a lack of randomness can manifest as security vulnerabilities.

Security

Please report security issues to security@siliconally.org, or by using one of the contact methods available on our Contact Us page.

Documentation

Overview

Package cryptorand provides a *rand.Rand implementation backed by the standard library's 'crypto/rand' package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New() *rand.Rand

New returns a *rand.Rand backed by the crypto/rand secure random number generator.

Types

type Source

type Source struct{}

Source implements math/rand.Source and math/rand.Source64, backed by the crypto/rand package.

func NewSource

func NewSource() *Source

NewSource returns an initialized crypto/rand-backed implementation of math/rand.Source.

func (*Source) Int63

func (s *Source) Int63() int64

Int63 returns a non-negative cryptographically secure random 63-bit integer as an int64.

func (*Source) Seed

func (*Source) Seed(_ int64)

Seed is a no-op, as the crytographically secure random number generated cannot be seeded.

func (*Source) Uint64

func (*Source) Uint64() uint64

Uint64 returns a cruptographically secure random 64-bit value as a uint64.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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