cryptosource

package module
v0.0.0-...-cb635f3 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2024 License: Unlicense Imports: 4 Imported by: 3

README

math/rand outputs are predictable, which can cause unexpected security issues in a number of scenarios. Because of that, it's best practice to default to crypto/rand unless a strong reason to use math/rand is present.

However, crypto/rand doesn't provide the same helper functions as math/rand.

Package cryptosource provides a crypto/rand-backed math/rand.Source, so the convenient math/rand methods can be used with the security of crypto/rand.

package main

import (
	"fmt"
	"math/rand"

	"filippo.io/mostly-harmless/cryptosource"
)

func main() {
	r := rand.New(cryptosource.New())
	fmt.Println(r.Float32())
}

Try this on the Go Playground.

The code is released in the Public Domain (and comes with a fallback license with no requirements) so you can just copy-paste it if you want. It's short.

Documentation

Overview

Package cryptosource provides a math/rand Source that draws random numbers from crypto/rand.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New() rand.Source

New returns a math/rand.Source64 that generates random numbers by reading bytes from crypto/rand.Reader.

func NewFromReader

func NewFromReader(r io.Reader) rand.Source

NewFromReader returns a math/rand.Source64 that generates random numbers by reading bytes from the io.Reader r.

Types

This section is empty.

Jump to

Keyboard shortcuts

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