-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f11f09e
commit 95d6bac
Showing
9 changed files
with
324 additions
and
2 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,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 |
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,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 }} |
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,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: ./... |
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,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 |
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,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. | ||
|
||
|
||
|
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,3 @@ | ||
module github.com/imusmanmalik/randomizer | ||
|
||
go 1.20 |
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,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 | ||
} |
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,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) | ||
} | ||
} |