Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(util): update generate random str and simple password functions #72

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 20 additions & 28 deletions pkg/util/random.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,38 @@
package util

import (
"crypto/rand"
"encoding/base64"
"errors"
mathRand "math/rand"
"math/rand"
"regexp"
)

const (
minioSecretIDLength = 40
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
specialBytes = "!@#$%^&*()_+-=[]{}\\|;':\",.<>/?`~"
numBytes = "0123456789"
)

// GenerateSecretAccessKey - generate random base64 numeric value from a random seed.
func GenerateSecretAccessKey() (string, error) {
rb := make([]byte, minioSecretIDLength)
if _, e := rand.Read(rb); e != nil {
return "", errors.New("could not generate Secret Key")
}

return string(Encode(rb)), nil
}

// Encode queues message
func Encode(value []byte) []byte {
length := len(value)
encoded := make([]byte, base64.URLEncoding.EncodedLen(length))
base64.URLEncoding.Encode(encoded, value)
return encoded
}

// GenerateRandomStr - generate random string for given length
func GenerateRandomStr(length int) string {
letterBytes := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
// GenerateRandomStr generates a random string of the specified length.
// The string can contain letters, special characters, and numbers based on the flags provided.
func GenerateRandomStr(length int, useLetters bool, useSpecial bool, useNum bool) string {
bytes := make([]byte, length)
for i := range bytes {
bytes[i] = letterBytes[mathRand.Intn(len(letterBytes))]
if useLetters {
bytes[i] = letterBytes[rand.Intn(len(letterBytes))]
} else if useSpecial {
bytes[i] = specialBytes[rand.Intn(len(specialBytes))]
} else if useNum {
bytes[i] = numBytes[rand.Intn(len(numBytes))]
}
}

return string(bytes)
}

// GenerateSimplePassword generates a simple password of the specified length.
// The password will contain only letters and numbers.
func GenerateSimplePassword(length int) string {
return GenerateRandomStr(length, true, false, true)
}

// RemoveSpecialCharacter - remove special character
func RemoveSpecialCharacter(str string) string {
regex := regexp.MustCompile("[^a-zA-Z0-9]+")
Expand Down
35 changes: 31 additions & 4 deletions pkg/util/random_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
package util

import "testing"
import (
"testing"
)

func Test_generateRandomStr(t *testing.T) {
str := GenerateRandomStr(5)
t.Log(str)
func TestGenerateRandomStr(t *testing.T) {
length := 10
useLetters := true
useSpecial := true
useNum := true

randomStr := GenerateRandomStr(length, useLetters, useSpecial, useNum)

if len(randomStr) != length {
t.Errorf("Generated random string length is incorrect. Expected: %d, Got: %d", length, len(randomStr))
}

// Add additional assertions here if needed

t.Logf("Generated random string: %s", randomStr)
}

func TestGenerateSimplePassword(t *testing.T) {
length := 8
password := GenerateSimplePassword(length)

if len(password) != length {
t.Errorf("Generated password length is incorrect. Expected: %d, Got: %d", length, len(password))
}

// Add additional assertions here if needed

t.Logf("Generated password: %s", password)
}
Loading