From e773748e9fa33178dbf66b5a6b3bd31ef66ff710 Mon Sep 17 00:00:00 2001 From: imusmanmalik Date: Wed, 10 May 2023 11:46:25 +0200 Subject: [PATCH] fix: Typo Closes #4. --- .github/workflows/release.yaml | 15 ++++++----- README.md | 35 +++++++++++++++---------- randomizer.go | 48 ++++++++++++++++++++++------------ randomizer_test.go | 41 ++++++++++++++++++++--------- 4 files changed, 89 insertions(+), 50 deletions(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 3842d4e..8e2a554 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -26,7 +26,7 @@ jobs: go-version: 1.20 - name: Release - id: release_output + id: semrel uses: go-semantic-release/action@v1 with: changelog-generator-opt: "emojis=true" @@ -35,11 +35,12 @@ jobs: 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 }}" + echo "Version: ${{ steps.semrel.outputs.version }}" + echo "Major Version: ${{ steps.semrel.outputs.version_major }}" + echo "Minor Version: ${{ steps.semrel.outputs.version_minor }}" + echo "Patch Version: ${{ steps.semrel.outputs.version_patch }}" + echo "Changelog: ${{ steps.semrel.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 }} + if: steps.semrel.outputs.version != '' + run: GOPROXY=proxy.golang.org go list -m github.com/imusmanmalik/randomizer@${{ steps.semrel.outputs.version }} diff --git a/README.md b/README.md index 834fdfb..ac493be 100644 --- a/README.md +++ b/README.md @@ -23,17 +23,26 @@ import ( ) 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) + // Generate a random integer between 1 and 100 + randomInt, err := randomizer.RandomInt(1, 100) + if err != nil { + panic(err) + } + fmt.Println(randomInt) + + // Generate a random slice of bytes with length 16 + randomBytes, err := randomizer.RandomBytes(16) + if err != nil { + panic(err) + } + fmt.Println(randomBytes) + + // Generate a random string with length 32 + randomString, err := randomizer.RandomString(32) + if err != nil { + panic(err) + } + fmt.Println(randomString) } ``` @@ -42,6 +51,7 @@ func main() { ```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. @@ -53,6 +63,3 @@ This library is licensed under Apache 2.0 License. See the [LICENSE file](https: ## 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/randomizer.go b/randomizer.go index 1d40a84..060dcc7 100644 --- a/randomizer.go +++ b/randomizer.go @@ -23,30 +23,44 @@ import ( "math/big" ) -// Generator is a struct that holds the random generator's configuration. -type Generator struct { -} +// RandomInt generates a cryptographically secure random integer between min and max (inclusive) +func RandomInt(min, max int64) (int64, error) { + // Create a new big.Int to hold the range of possible values + rangeSize := big.NewInt(max - min + 1) + + // Generate a cryptographically secure random value within the range + randomValue, err := rand.Int(rand.Reader, rangeSize) + if err != nil { + return 0, err + } -// NewGenerator returns a new instance of the random generator. -func NewGenerator() *Generator { - return &Generator{} + // Convert the random value to an int64 and add the minimum value to shift the range + return randomValue.Int64() + min, nil } -// 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) +// RandomBytes generates a slice of cryptographically secure random bytes with a given length +func RandomBytes(length int) ([]byte, error) { + // Create a slice to hold the random bytes + randomBytes := make([]byte, length) + + // Generate the random bytes using the crypto/rand package + _, err := rand.Read(randomBytes) if err != nil { - return 0, err + return nil, err } - return int(r.Int64()), nil + + return randomBytes, 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)) +// RandomString generates a cryptographically secure random string with a given length +func RandomString(length int) (string, error) { + // Generate a slice of random bytes with the given length + randomBytes, err := RandomBytes(length) if err != nil { - return 0, err + return "", err } - return float64(r.Int64()) / 1e17, nil + + randomString := string(randomBytes) + + return randomString, nil } diff --git a/randomizer_test.go b/randomizer_test.go index 74db16e..acb1779 100644 --- a/randomizer_test.go +++ b/randomizer_test.go @@ -22,24 +22,41 @@ import ( "testing" ) -func TestGenerator(t *testing.T) { - g := NewGenerator() +func TestGenerateRandomInt(t *testing.T) { + // Generate a random integer between 1 and 100 + randomInt, err := RandomInt(1, 100) + if err != nil { + t.Errorf("Error generating random integer: %v", err) + } + + // Check that the generated integer is within the range + if randomInt < 1 || randomInt > 100 { + t.Errorf("Generated integer %v is not within the specified range", randomInt) + } +} - // Test Intn function. - n, err := g.Intn(100) +func TestGenerateRandomBytes(t *testing.T) { + // Generate a random byte slice with length 16 + randomBytes, err := RandomBytes(16) if err != nil { - t.Fatalf("Intn returned error: %s", err) + t.Errorf("Error generating random bytes: %v", err) } - if n < 0 || n >= 100 { - t.Fatalf("Intn returned invalid value: %d", n) + + // Check that the length of the generated byte slice is correct + if len(randomBytes) != 16 { + t.Errorf("Generated byte slice has incorrect length: expected 16, got %v", len(randomBytes)) } +} - // Test Float64 function. - f, err := g.Float64() +func TestGenerateRandomString(t *testing.T) { + // Generate a random string with length 32 + randomString, err := RandomString(32) if err != nil { - t.Fatalf("Float64 returned error: %s", err) + t.Errorf("Error generating random string: %v", err) } - if f < 0.0 || f >= 1.0 { - t.Fatalf("Float64 returned invalid value: %f", f) + + // Check that the length of the generated string is correct + if len(randomString) != 32 { + t.Errorf("Generated string has incorrect length: expected 32, got %v", len(randomString)) } }