Skip to content
/ srand Public

Utility and convenience functions for working with secure randoms in Go.

License

Notifications You must be signed in to change notification settings

toashd/srand

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

srand

srand contains utility and convenience functions for working with secure randoms.

GoDoc BuildStatus

Description

srand helps generating secure random numbers, non-consecutive and non-predictable keys (e.g. session keys, CSRF, and HMACS) using crypto/rand and cryptographically secure pseudo-random number generator (CSPRNG).

API

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

Generates raw bytes for another cryptographic function, such as HMAC keys.

func GenerateRandomString(s int) (string, error)

Generates keys for session IDs, CSRF tokens etc. It is base64 URL encoded, providing secure strings that can be used in file-names, templates, HTTP headers and to minimize encoding overhead compared to hex encoding.

func GenerateRandomInt(n int64) (int64, error)

Generates a securely generated random number output between 0 and n.

func GenerateRandomIntRange(m, n int64) (int64, error)

Generates a securely generated random number output in range [m, n).

func GenerateRandomFloat() (float64, error)

Generates a securely generated random floating point number output in range [0, 1).

func GenerateRandomFloatRange(m, n float64) (float64, error)

Generates a securely generated random floating point number output in range [m, n).

func GenerateUUID(n int64) (*big.Int, error)

Generates a securely generated UUID compliant to RFC 4122.

Getting Started

1: Download the package

go get github.com/toashd/srand

2: Import srand to your Go project

import "github.com/toashd/srand"

Example

package main

import (
  "fmt"
  "github.com/toashd/srand"
)

func main() {
	// Example: generate a 44 byte, base64 encoded, url-safe token.
	token, err := srand.GenerateRandomString(32)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("%v\n", token)

	// Example: generate a random number output between [0, 100).
	rand, err := GenerateRandomInt(100)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("%v\n", rand)

	// Example: generate a random floating point number output between [0, 1).
	rand, err := GenerateRandomFloat()
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("%v\n", rand)

	// Example: generate a UUID according to RFC 4122.
	uuid, err := GenerateUUID()
	if err != nil {
		fmt.Printf("error: %v\n", err)
	}
	fmt.Printf("%v\n", uuid)
}

Contribution

Let me know your utils or feel free to suggest any kind of improvements, refactorings. Just file an issue, fork and submit a pull request.

Get in touch

Tobias Schmid, toashd@gmail.com, @toashd, toashd.com

License

srand is available under the MIT license. See the LICENSE file for more info.

About

Utility and convenience functions for working with secure randoms in Go.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages