Code Monkey home page Code Monkey logo

gopherng's Introduction

gopherng

Seeded pseudorandom Reader and rand.Source

Go Reference

Note

This package was implemented for the purposes of the CubeDance game and may not be applicable for your purposes. It has not been independently audited for security.

Read random bytes from PseudorandomReader

This package includes a pseudorandom Reader. It initializes, based on a provided seed, an XChaCha20 stream cipher which is used to encrypt 0s in order to generate random bytes.

Internally the seed is hashed to produce a key and nonce to initialize the XChaCha20 cipher. This is not a substitute for using a sufficiently high-entropy seed or sufficiently stretching a lower-entropy seed.

// Create PseudorandomReader with a high-entropy seed.
r := gopherng.NewReader([]byte{1, 2, 3, /* ... use a high-entropy seed */})

p := make([]byte, 256)
_, err := r.Read(p)
if err != nil {
    /* ... */
}

fmt.Println(p)
// => [43 98 21 80 122 33 48 91 135 248 242 ... ]

Generate random values with PseudorandomSource

This package also includes a pseudorandom rand.Source which can be used to initialize a rand.Rand (from math/rand) like so:

// Create PseudorandomSource with a high-entropy seed.
s := gopherng.NewSource([]byte{1, 2, 3, /* ... use a high-entropy seed */})

// Create a rand.Rand from the Source.
r := rand.New(s)

fmt.Println(r.Float64())
// => 0.7124063346129034

gopherng's People

Contributors

ethanent avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.