mirror of
https://github.com/rwinkhart/go-boilerplate.git
synced 2026-09-05 16:47:20 -04:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
03e6942724 | ||
|
|
df1fb21b23 | ||
|
|
10ee4f91fc |
@@ -7,6 +7,9 @@ var Home, _ = os.UserHomeDir()
|
|||||||
const (
|
const (
|
||||||
AnsiBold = "\033[1m"
|
AnsiBold = "\033[1m"
|
||||||
AnsiError = "\033[38;5;9m"
|
AnsiError = "\033[38;5;9m"
|
||||||
|
AnsiWarning = "\033[38;5;3m"
|
||||||
|
AnsiBlue = "\033[38;5;4m"
|
||||||
|
AnsiGreen = "\033[38;5;2m"
|
||||||
AnsiReset = "\033[0m"
|
AnsiReset = "\033[0m"
|
||||||
|
|
||||||
ErrorRead = 101
|
ErrorRead = 101
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
module github.com/rwinkhart/go-boilerplate
|
module github.com/rwinkhart/go-boilerplate
|
||||||
|
|
||||||
go 1.24.3
|
go 1.25.5
|
||||||
|
|
||||||
require golang.org/x/term v0.32.0
|
require golang.org/x/term v0.38.0
|
||||||
|
|
||||||
require golang.org/x/sys v0.33.0 // indirect
|
require golang.org/x/sys v0.39.0 // indirect
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
|
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
|
||||||
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||||
golang.org/x/term v0.32.0 h1:DR4lr0TjUs3epypdhTOkMmuF5CDFJ/8pOnbzMZPQ7bg=
|
golang.org/x/term v0.38.0 h1:PQ5pkm/rLO6HnxFR7N2lJHOZX6Kez5Y1gDSJla6jo7Q=
|
||||||
golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ=
|
golang.org/x/term v0.38.0/go.mod h1:bSEAKrOT1W+VSu9TSCMtoGEOUcKxOKgl3LE5QEF/xVg=
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package stringy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"math/big"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// StringGen generates a random string of a specified length and complexity.
|
||||||
|
// Requires: complexity (minimum percentage of special characters to be returned in the generated string; set to 0 to generate a simple string),
|
||||||
|
// complexCharsetLevel (1 = safe for filenames, 2 = safe for most password entries, 3 = safe only for well-made password entries)
|
||||||
|
func StringGen(length int, complexity float64, complexCharsetLevel uint8) string {
|
||||||
|
var actualSpecialChars int // track the number of special characters in the generated string
|
||||||
|
var minSpecialChars int // track the minimum number of special characters to accept
|
||||||
|
var extendedCharset string // additions to character set used for complex strings
|
||||||
|
|
||||||
|
charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" // default character set used for all strings
|
||||||
|
const extendedCharsetFiles = "!#$%&+,-.;=@_~^()[]{}`'" // additional special characters for complex strings (safe in file names)
|
||||||
|
const extendedCharsetMostPassword = "*:><?|" // additional special characters for complex strings (NOT safe in file names)
|
||||||
|
const extendedCharsetSpecialPassword = "\"/\\" // additional special characters for complex strings (NOT safe in file names)
|
||||||
|
|
||||||
|
if complexity > 0 {
|
||||||
|
minSpecialChars = int(math.Round(float64(length) * complexity)) // determine minimum number of special characters to accept
|
||||||
|
switch complexCharsetLevel {
|
||||||
|
case 1:
|
||||||
|
extendedCharset = extendedCharsetFiles
|
||||||
|
case 2:
|
||||||
|
extendedCharset = extendedCharsetMostPassword + extendedCharsetFiles[:len(extendedCharsetFiles)-9]
|
||||||
|
case 3:
|
||||||
|
extendedCharset = extendedCharsetFiles + extendedCharsetMostPassword + extendedCharsetSpecialPassword
|
||||||
|
}
|
||||||
|
charset += extendedCharset
|
||||||
|
}
|
||||||
|
|
||||||
|
// loop until a string of the desired complexity is generated
|
||||||
|
for {
|
||||||
|
// generate a random string
|
||||||
|
result := make([]byte, length)
|
||||||
|
for i := range result {
|
||||||
|
val, _ := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
|
||||||
|
result[i] = charset[val.Int64()]
|
||||||
|
}
|
||||||
|
|
||||||
|
// return early if the string is not complex
|
||||||
|
if complexity <= 0 {
|
||||||
|
return string(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
// count the number of special characters in the generated string
|
||||||
|
for _, char := range string(result) {
|
||||||
|
if strings.ContainsRune(extendedCharset, char) {
|
||||||
|
actualSpecialChars++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// return the generated string if it contains enough special characters
|
||||||
|
if actualSpecialChars >= minSpecialChars {
|
||||||
|
return string(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
// reset special character counter
|
||||||
|
fmt.Println("Regenerating string until desired complexity is achieved...")
|
||||||
|
actualSpecialChars = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user