commit 81c0625d275cc1ce44d62e047cf9b12db1bebc7a Author: Eauldane Date: Sun Aug 31 04:53:42 2025 +0100 Initial commit - secure RandomStringGenerator implementation diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fae3642 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +bin +.idea \ No newline at end of file diff --git a/SnowcloakUtils.go b/SnowcloakUtils.go new file mode 100644 index 0000000..2f605f4 --- /dev/null +++ b/SnowcloakUtils.go @@ -0,0 +1,26 @@ +package SnowcloakUtils + +import ( + "crypto/rand" // Probably better than math/rand for this + "log" +) + +func GenerateRandomString(length int) string { + // C# had optional parameters that allowed lowercase for chardata and gpose lobbies, Go doesn't. + // We can probably get away with just uppercase. + allowedArray := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") + random := make([]byte, length) + _, err := rand.Read(random) + if err != nil { + log.Fatalf("Couldn't generate secure slice: %s", err) + } + allowedLength := len(allowedArray) + i := 0 + charArray := make([]rune, length) + for i < length { + charArray[i] = allowedArray[int(random[i])%allowedLength] // Casting random[i] due to type mismatch + i++ + } + + return string(charArray) +} diff --git a/SnowcloakUtils_test.go b/SnowcloakUtils_test.go new file mode 100644 index 0000000..f5d4643 --- /dev/null +++ b/SnowcloakUtils_test.go @@ -0,0 +1,17 @@ +package SnowcloakUtils + +import ( + "fmt" + "testing" +) + +func TestGenerateRandomString(t *testing.T) { + fmt.Println("Test: 8 chars") + fmt.Println(GenerateRandomString(8)) + fmt.Println("Test: 10 chars") + fmt.Println(GenerateRandomString(10)) + fmt.Println("Test: 15 chars") + fmt.Println(GenerateRandomString(15)) + fmt.Println("Test: 30 chars") + fmt.Println(GenerateRandomString(30)) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..4ec0688 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module SnowcloakUtils + +go 1.25