csprng

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2023 License: BSD-2-Clause Imports: 6 Imported by: 51

Documentation

Overview

Package csprng wraps the golang crypto/rand package so that we can use different random number generators interchangeably when the need arises.

Package csprng wraps the golang crypto/rand package so that we can use different random number generators interchangeably when the need arises.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate(size int, rng io.Reader) ([]byte, error)

Generate a byte slice of size and return the result Note use of io.Reader interface, as Source implements that, we only require a Read function for these utilities.

func GenerateInGroup

func GenerateInGroup(prime []byte, size int, rng io.Reader) ([]byte,
	error)

GenerateInGroup creates a byte slice of at most size inside the given prime group and returns the result.

WARNING: This can generate outside the subgroup generated by g if g does

not generate the entire space of 1 to p-1, so you will have to
check for and compensate for this case. Similarly, using such
generators is not valid for signatures. For details, see:
“Generating ElGamal signatures without knowing the secret key” by
Bleichenbacher, OR chapter 11 of the Handbook of Applied Crypto,
note 11.67

func InGroup

func InGroup(sample, prime []byte) bool

InGroup returns true if the sample is non-zero and less than the prime. This is useful for testing if a generated number is inside the modular cyclic group defined by the prime. NOTE: This code assumes byte 0 is the Most significant byte (MSB)

Types

type Source

type Source interface {
	// Read returns a slice of len(b) size bytes from the random number
	// generator, or an error if one occurs
	Read(b []byte) (int, error)
	// SetSeed sets the internal state of the random number generator, or an error
	SetSeed(seed []byte) error
}

Source is the common interface for all cryptographically secure random number generators

func NewSystemRNG

func NewSystemRNG() Source

NewSystemRNG gets the systemRNG as the interface

type SourceConstructor

type SourceConstructor func() Source

Defines the constructor of a source

type SystemRNG

type SystemRNG struct{}

SystemRNG uses the golang CSPRNG

func (*SystemRNG) Read

func (s *SystemRNG) Read(b []byte) (int, error)

Read calls the crypto/rand Read function and returns the values

func (*SystemRNG) SetSeed

func (s *SystemRNG) SetSeed(seed []byte) error

SetSeed has not effect on the system reader

Jump to

Keyboard shortcuts

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