Skip to content
This repository has been archived by the owner on Apr 24, 2023. It is now read-only.

Commit

Permalink
feat: Package rework
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
Taliesin Millhouse committed Nov 28, 2020
1 parent c29e008 commit dc76139
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 16 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -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 ./...
9 changes: 5 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
*.out

# Text editors/IDEs
.vscode
.vscode/
__debug_bin
.idea
.idea/

# Directories.
dist
bin/
dist/

# Env files
# Env files.
*.env
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
# little rand
## 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.
16 changes: 8 additions & 8 deletions crypto.go → crypto_rand.go
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
}
Expand Down
33 changes: 33 additions & 0 deletions crypto_rand_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/gofor-little/rand
module github.com/gofor-little/xrand

go 1.13
go 1.15
34 changes: 34 additions & 0 deletions math_rand.go
Original file line number Diff line number Diff line change
@@ -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
}
33 changes: 33 additions & 0 deletions math_rand_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
2 changes: 1 addition & 1 deletion uuid.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rand
package xrand

import (
"crypto/rand"
Expand Down
18 changes: 18 additions & 0 deletions uuid_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}

0 comments on commit dc76139

Please sign in to comment.