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

Refactor [Common] [Utils] #792

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 17 additions & 7 deletions common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package common

import (
"fmt"
"github.com/google/uuid"
"html/template"
"log"
"math/rand"
Expand All @@ -13,6 +12,8 @@ import (
"strconv"
"strings"
"time"

"github.com/google/uuid"
)

func OpenBrowser(url string) {
Expand Down Expand Up @@ -137,15 +138,23 @@ func GetUUID() string {

const keyChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

func init() {
rand.Seed(time.Now().UnixNano())
// Remove this function as it's no longer needed. (depcrecated)
// Note: This function required go 1.20+
// func init() {
// rand.Seed(time.Now().UnixNano())
// }

// newRand creates a new instance of a random generator.
func newRand() *rand.Rand {
return rand.New(rand.NewSource(time.Now().UnixNano()))
}

// GenerateKey creates a new key string.
func GenerateKey() string {
rand.Seed(time.Now().UnixNano())
localRand := newRand() // Use the local random generator
key := make([]byte, 48)
for i := 0; i < 16; i++ {
key[i] = keyChars[rand.Intn(len(keyChars))]
key[i] = keyChars[localRand.Intn(len(keyChars))]
}
uuid_ := GetUUID()
for i := 0; i < 32; i++ {
Expand All @@ -158,11 +167,12 @@ func GenerateKey() string {
return string(key)
}

// GetRandomString generates a random string of a specified length.
func GetRandomString(length int) string {
rand.Seed(time.Now().UnixNano())
localRand := newRand() // Use the local random generator
key := make([]byte, length)
for i := 0; i < length; i++ {
key[i] = keyChars[rand.Intn(len(keyChars))]
key[i] = keyChars[localRand.Intn(len(keyChars))]
}
return string(key)
}
Expand Down