This repository has been archived by the owner on Apr 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
Showing
10 changed files
with
183 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package rand | ||
package xrand | ||
|
||
import ( | ||
"crypto/rand" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |