Skip to content

Commit

Permalink
feat: Initial Release
Browse files Browse the repository at this point in the history
  • Loading branch information
imusmanmalik committed Apr 5, 2023
1 parent f11f09e commit 95d6bac
Show file tree
Hide file tree
Showing 9 changed files with 324 additions and 2 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -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
45 changes: 45 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -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 }}
37 changes: 37 additions & 0 deletions .github/workflows/scan.yaml
Original file line number Diff line number Diff line change
@@ -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: ./...
35 changes: 35 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
60 changes: 58 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.



3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/imusmanmalik/randomizer

go 1.20
52 changes: 52 additions & 0 deletions randomizer.go
Original file line number Diff line number Diff line change
@@ -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
}
45 changes: 45 additions & 0 deletions randomizer_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 95d6bac

Please sign in to comment.