From dc761398c8be5a809da8226a7c18b1afcfc42689 Mon Sep 17 00:00:00 2001 From: Taliesin Millhouse Date: Sat, 28 Nov 2020 18:12:51 +1100 Subject: [PATCH] feat: Package rework - Renamed package to xrand to not clash with standard library rand packages. - Added GitHub action to run tests. - Updated .gitignore. - Renamed several methods. - Fixed typos in comments. - Added tests for all methods. - Updated go module version to 1.15. - Updated README.md --- .github/workflows/go.yml | 33 +++++++++++++++++++++++++++++++++ .gitignore | 9 +++++---- README.md | 17 ++++++++++++++++- crypto.go => crypto_rand.go | 16 ++++++++-------- crypto_rand_test.go | 33 +++++++++++++++++++++++++++++++++ go.mod | 4 ++-- math_rand.go | 34 ++++++++++++++++++++++++++++++++++ math_rand_test.go | 33 +++++++++++++++++++++++++++++++++ uuid.go | 2 +- uuid_test.go | 18 ++++++++++++++++++ 10 files changed, 183 insertions(+), 16 deletions(-) create mode 100644 .github/workflows/go.yml rename crypto.go => crypto_rand.go (61%) create mode 100644 crypto_rand_test.go create mode 100644 math_rand.go create mode 100644 math_rand_test.go create mode 100644 uuid_test.go diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 0000000..4947d4b --- /dev/null +++ b/.github/workflows/go.yml @@ -0,0 +1,33 @@ +name: Go + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +env: + SIR_INSTALL_DIRECTORY: . + SIR_REGISTRY_BUCKET: test + +jobs: + + build: + name: Build + runs-on: ubuntu-latest + steps: + + - name: Set up Go 1.x + uses: actions/setup-go@v2 + with: + go-version: ^1.15 + id: go + + - name: Check out code into the Go module directory + uses: actions/checkout@v2 + + - name: Get dependencies + run: | + go get -v -t -d ./... + - name: Test + run: go test -v ./... \ No newline at end of file diff --git a/.gitignore b/.gitignore index 888aba4..23ebd6a 100644 --- a/.gitignore +++ b/.gitignore @@ -12,12 +12,13 @@ *.out # Text editors/IDEs -.vscode +.vscode/ __debug_bin -.idea +.idea/ # Directories. -dist +bin/ +dist/ -# Env files +# Env files. *.env \ No newline at end of file diff --git a/README.md b/README.md index 7bf336b..215098a 100644 --- a/README.md +++ b/README.md @@ -1 +1,16 @@ -# little rand \ No newline at end of file +## A package that extends math/rand and crypto/rand in the standard library + +![GitHub tag (latest SemVer pre-release)](https://img.shields.io/github/v/tag/gofor-little/xrand?include_prereleases) +![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/gofor-little/xrand) +[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://raw.githubusercontent.com/gofor-little/xrand/main/LICENSE) +![GitHub Workflow Status](https://img.shields.io/github/workflow/status/gofor-little/xrand/Go) +[![Go Report Card](https://goreportcard.com/badge/github.com/gofor-little/xrand)](https://goreportcard.com/report/github.com/gofor-little/xrand) +[![PkgGoDev](https://pkg.go.dev/badge/github.com/gofor-little/xrand)](https://pkg.go.dev/github.com/gofor-little/xrand) + +### Introduction +* Generate random strings and byte slices +* Cryptographically secure generation +* No dependencies outside the standard library + +### Testing +Run ```go test ./...``` in the root directory. \ No newline at end of file diff --git a/crypto.go b/crypto_rand.go similarity index 61% rename from crypto.go rename to crypto_rand.go index dc42c86..2356931 100644 --- a/crypto.go +++ b/crypto_rand.go @@ -1,11 +1,11 @@ -package rand +package xrand import "crypto/rand" -// GenerateCryptoBytes generates a cryptographically secure byte slice. -// Returns an error if the systems cryptographically secure random number generator fails, +// RandomCryptoBytes generates a cryptographically secure random byte slice. +// Returns an error if the systems cryptographically secure random generator fails, // in which case the caller should not continue. -func GenerateCryptoBytes(length uint) ([]byte, error) { +func RandomCryptoBytes(length uint) ([]byte, error) { key := make([]byte, length) _, err := rand.Read(key) @@ -16,13 +16,13 @@ func GenerateCryptoBytes(length uint) ([]byte, error) { return key, nil } -// GenerateCryptoString generates a cryptographically secure byte slice. -// Returns an error if the systems cryptographically secure random number generator fails, +// RandomCryptoString generates a cryptographically secure random string. +// Returns an error if the systems cryptographically secure random generator fails, // in which case the caller should not continue. -func GenerateCryptoString(length uint) (string, error) { +func RandomCryptoString(length uint) (string, error) { const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-" - bytes, err := GenerateCryptoBytes(length) + bytes, err := RandomCryptoBytes(length) if err != nil { return "", err } diff --git a/crypto_rand_test.go b/crypto_rand_test.go new file mode 100644 index 0000000..6cafa91 --- /dev/null +++ b/crypto_rand_test.go @@ -0,0 +1,33 @@ +package xrand_test + +import ( + "testing" + + "github.com/gofor-little/xrand" +) + +func TestRandomCryptoBytes(t *testing.T) { + var length uint = 16 + + data, err := xrand.RandomCryptoBytes(length) + if err != nil { + t.Fatalf("failed to generate cryptographically secure random bytes: %v", err) + } + + if uint(len(data)) != length { + t.Fatalf("xrand.RandomCryptoBytes returned incorrect length, wanted: %d, got: %d", length, len(data)) + } +} + +func TestRandomCryptoString(t *testing.T) { + var length uint = 16 + + data, err := xrand.RandomCryptoString(length) + if err != nil { + t.Fatalf("failed to generate cryptographically secure random string: %v", err) + } + + if uint(len(data)) != length { + t.Fatalf("xrand.RandomCryptoString returned incorrect length, wanted: %d, got: %d", length, len(data)) + } +} diff --git a/go.mod b/go.mod index b2989fa..099b5e3 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/gofor-little/rand +module github.com/gofor-little/xrand -go 1.13 +go 1.15 diff --git a/math_rand.go b/math_rand.go new file mode 100644 index 0000000..b3f5506 --- /dev/null +++ b/math_rand.go @@ -0,0 +1,34 @@ +package xrand + +import "math/rand" + +// RandomMathBytes generates a random byte slice. This is NOT cryptographically +// secure. If you require the bytes to be cryptographically secure, see +// xrand.RandomCryptoBytes. +func RandomMathBytes(length uint) ([]byte, error) { + key := make([]byte, length) + + _, err := rand.Read(key) + if err != nil { + return nil, err + } + + return key, nil +} + +// RandomMathString generates a random string. This is NOT cryptographically secure. +// If you require the string to be cryptographically secure see, xrand.RandomCryptoString. +func RandomMathString(length uint) (string, error) { + const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-" + + bytes, err := RandomMathBytes(length) + if err != nil { + return "", err + } + + for i, b := range bytes { + bytes[i] = letters[b%byte(len(letters))] + } + + return string(bytes), nil +} diff --git a/math_rand_test.go b/math_rand_test.go new file mode 100644 index 0000000..bf6ad1a --- /dev/null +++ b/math_rand_test.go @@ -0,0 +1,33 @@ +package xrand_test + +import ( + "testing" + + "github.com/gofor-little/xrand" +) + +func TestRandomMathBytes(t *testing.T) { + var length uint = 16 + + data, err := xrand.RandomMathBytes(length) + if err != nil { + t.Fatalf("failed to generate random bytes: %v", err) + } + + if uint(len(data)) != length { + t.Fatalf("xrand.RandomMathBytes returned incorrect length, wanted: %d, got: %d", length, len(data)) + } +} + +func TestRandomMathString(t *testing.T) { + var length uint = 16 + + data, err := xrand.RandomMathString(length) + if err != nil { + t.Fatalf("failed to generate random string: %v", err) + } + + if uint(len(data)) != length { + t.Fatalf("xrand.RandomMathString returned incorrect length, wanted: %d, got: %d", length, len(data)) + } +} diff --git a/uuid.go b/uuid.go index a973f80..46c0f48 100644 --- a/uuid.go +++ b/uuid.go @@ -1,4 +1,4 @@ -package rand +package xrand import ( "crypto/rand" diff --git a/uuid_test.go b/uuid_test.go new file mode 100644 index 0000000..415e575 --- /dev/null +++ b/uuid_test.go @@ -0,0 +1,18 @@ +package xrand_test + +import ( + "testing" + + "github.com/gofor-little/xrand" +) + +func TestGenerateUUID(t *testing.T) { + uuid, err := xrand.GenerateUUID() + if err != nil { + t.Fatalf("failed to generate v4 UUID: %v", err) + } + + if len(uuid) != 36 { + t.Fatalf("xrand.GenerateUUID returned incorrect length, wanted: %d, got: %d", 36, len(uuid)) + } +}