From 95d6bacbacb328a553a39726401f520c343cf025 Mon Sep 17 00:00:00 2001 From: imusmanmalik Date: Wed, 5 Apr 2023 11:12:32 +0200 Subject: [PATCH] feat: Initial Release --- .github/workflows/build.yaml | 28 ++++++++++++++++ .github/workflows/release.yaml | 45 +++++++++++++++++++++++++ .github/workflows/scan.yaml | 37 +++++++++++++++++++++ .github/workflows/test.yaml | 35 ++++++++++++++++++++ .gitignore | 21 ++++++++++++ README.md | 60 ++++++++++++++++++++++++++++++++-- go.mod | 3 ++ randomizer.go | 52 +++++++++++++++++++++++++++++ randomizer_test.go | 45 +++++++++++++++++++++++++ 9 files changed, 324 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/build.yaml create mode 100644 .github/workflows/release.yaml create mode 100644 .github/workflows/scan.yaml create mode 100644 .github/workflows/test.yaml create mode 100644 go.mod create mode 100644 randomizer.go create mode 100644 randomizer_test.go diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml new file mode 100644 index 0000000..1851b36 --- /dev/null +++ b/.github/workflows/build.yaml @@ -0,0 +1,28 @@ +name: CI Build + +on: + push: + branches: + - main + pull_request: + branches: + - main + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + +jobs: + build: + name: Build + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Setup Go + uses: actions/setup-go@v2 + with: + go-version: 1.20 + + - name: Build + run: go build diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..3842d4e --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,45 @@ +name: CI Release + +on: + push: + branches: + - main + workflow_run: + workflows: ["CI Scan"] + types: + - completed + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + +jobs: + release: + name: Release + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Setup Go + uses: actions/setup-go@v2 + with: + go-version: 1.20 + + - name: Release + id: release_output + uses: go-semantic-release/action@v1 + with: + changelog-generator-opt: "emojis=true" + changelog-file: "CHANGELOG.md" + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - run: | + echo "Version: ${{ steps.release_output.outputs.version }}" + echo "Major Version: ${{ steps.release_output.outputs.version_major }}" + echo "Minor Version: ${{ steps.release_output.outputs.version_minor }}" + echo "Patch Version: ${{ steps.release_output.outputs.version_patch }}" + echo "Changelog: ${{ steps.release_output.outputs.changelog }}" + + - name: Publish to pkg.go.dev + run: GOPROXY=proxy.golang.org go list -m github.com/imusmanmalik/randomizer@${{ steps.release_output.outputs.version }} diff --git a/.github/workflows/scan.yaml b/.github/workflows/scan.yaml new file mode 100644 index 0000000..d122c47 --- /dev/null +++ b/.github/workflows/scan.yaml @@ -0,0 +1,37 @@ +name: CI Scan + +on: + pull_request: + branches: + - main + push: + branches: + - main + workflow_run: + workflows: ["CI Test"] + types: + - completed + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + +jobs: + scan: + name: Scan + runs-on: ubuntu-20.04 + steps: + - name: Check out Git repository + uses: actions/checkout@v3 + + - name: Run trivy for vulnerabilities + uses: aquasecurity/trivy-action@master + with: + scan-type: 'fs' + ignore-unfixed: true + format: 'github' + severity: 'HIGH,CRITICAL' + + - name: Run Gosec Security Scanner + uses: securego/gosec@master + with: + args: ./... diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..8f84827 --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,35 @@ +name: CI Test + +on: + pull_request: + branches: + - main + push: + branches: + - main + workflow_run: + workflows: ["CI Build"] + types: + - completed + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + +jobs: + tests: + name: Test + runs-on: ubuntu-latest + steps: + - name: Check out Git repository + uses: actions/checkout@v3 + + - uses: actions/setup-go@v3 + with: + go-version: '1.20' + + - name: Test + run: go test -race -v -coverprofile=profile.cov ./... + + - uses: shogo82148/actions-goveralls@v1 + with: + path-to-profile: profile.cov diff --git a/.gitignore b/.gitignore index 66fd13c..77ca39a 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,24 @@ # Dependency directories (remove the comment below to include it) # vendor/ + +### VisualStudioCode ### +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +!.vscode/*.code-snippets + +# Local History for Visual Studio Code +.history/ + +# Built Visual Studio Code Extensions +*.vsix + +### VisualStudioCode Patch ### +# Ignore all local history of files +.history +.ionide + +profile.cov diff --git a/README.md b/README.md index 50bee4e..a281f65 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,58 @@ -# randomzier -GoLang library for generating cryptographically secure random numbers using the crypto/rand package +# randomizer + +[![Coverage Status](https://coveralls.io/repos/github/imusmanmalik/randomizer/badge.svg?branch=main)](https://coveralls.io/github/imusmanmalik/randomizer?branch=main) [![CI Build](https://github.com/imusmanmalik/randomizer/actions/workflows/build.yaml/badge.svg)](https://github.com/imusmanmalik/randomizer/actions/workflows/build.yaml) [![CI Test](https://github.com/imusmanmalik/randomizer/actions/workflows/test.yaml/badge.svg)](https://github.com/imusmanmalik/randomizer/actions/workflows/test.yaml) [![CI Scan](https://github.com/imusmanmalik/randomizer/actions/workflows/scan.yaml/badge.svg)](https://github.com/imusmanmalik/randomizer/actions/workflows/scan.yaml) [![CI Release](https://github.com/imusmanmalik/randomizer/actions/workflows/release.yaml/badge.svg)](https://github.com/imusmanmalik/randomizer/actions/workflows/release.yaml) + +This is a GoLang library for generating cryptographically secure random numbers using the crypto/rand package. The library provides a simple API for generating random integers, bytes, and strings. + +## Installation + +To install the library, use the go get command: + +```shell +go get github.com/imusmanmalik/randomizer +``` + +## Usage + +To use the library, import it in your Go code: + +```go +import ( + "fmt" + "github.com/imusmanmalik/randomizer" +) + +func main() { + // Generate a random integer between 0 and 100 + n := yourpackage.RandomInt(100) + fmt.Println(n) + + // Generate a random byte slice with 16 bytes + b := yourpackage.RandomBytes(16) + fmt.Printf("%x\n", b) + + // Generate a random string with 10 characters + s := yourpackage.RandomString(10) + fmt.Println(s) +} +``` + +# Testing + +```shell +go test +``` +## Contributing + +Contributions are welcome! If you find a bug or have an idea for a new feature, please open an issue or submit a pull request on GitHub. + +## License + +This library is licensed under Apache 2.0 License. See the [LICENSE file](https://github.com/imusmanmalik/randomizer/blob/main/LICENSE) for details. + +## Acknowledgments + +This library was inspired by the math/rand package in the Go standard library, and the github.com/Pallinder/go-randomdata library. + + + diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1477dfd --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/imusmanmalik/randomizer + +go 1.20 diff --git a/randomizer.go b/randomizer.go new file mode 100644 index 0000000..1d40a84 --- /dev/null +++ b/randomizer.go @@ -0,0 +1,52 @@ +/* +# +# Copyright Usman Malik - https://github.com/imusmanmalik +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +*/ + +package randomizer + +import ( + "crypto/rand" + "math/big" +) + +// Generator is a struct that holds the random generator's configuration. +type Generator struct { +} + +// NewGenerator returns a new instance of the random generator. +func NewGenerator() *Generator { + return &Generator{} +} + +// Intn returns a cryptographically secure random integer between 0 and n. +func (g *Generator) Intn(n int) (int, error) { + max := big.NewInt(int64(n)) + r, err := rand.Int(rand.Reader, max) + if err != nil { + return 0, err + } + return int(r.Int64()), nil +} + +// Float64 returns a cryptographically secure random float64 between 0 and 1. +func (g *Generator) Float64() (float64, error) { + r, err := rand.Int(rand.Reader, big.NewInt(1e17)) + if err != nil { + return 0, err + } + return float64(r.Int64()) / 1e17, nil +} diff --git a/randomizer_test.go b/randomizer_test.go new file mode 100644 index 0000000..74db16e --- /dev/null +++ b/randomizer_test.go @@ -0,0 +1,45 @@ +/* +# +# Copyright Usman Malik - https://github.com/imusmanmalik +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +*/ + +package randomizer + +import ( + "testing" +) + +func TestGenerator(t *testing.T) { + g := NewGenerator() + + // Test Intn function. + n, err := g.Intn(100) + if err != nil { + t.Fatalf("Intn returned error: %s", err) + } + if n < 0 || n >= 100 { + t.Fatalf("Intn returned invalid value: %d", n) + } + + // Test Float64 function. + f, err := g.Float64() + if err != nil { + t.Fatalf("Float64 returned error: %s", err) + } + if f < 0.0 || f >= 1.0 { + t.Fatalf("Float64 returned invalid value: %f", f) + } +}