mirror of
https://github.com/rwinkhart/go-boilerplate.git
synced 2026-08-28 04:46:41 -04:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f70ff83e1e | ||
|
|
2fe21452c7 | ||
|
|
687518a0a6 | ||
|
|
8313a183b0 | ||
|
|
7a63a41116 | ||
|
|
b7a2ae3378 | ||
|
|
26686374bd | ||
|
|
29315c4b76 | ||
|
|
48e6abea8b | ||
|
|
e1bb0012a7 | ||
|
|
9ee213eeb4 |
+12
-7
@@ -6,10 +6,12 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
|
||||||
|
"github.com/rwinkhart/go-boilerplate/security"
|
||||||
)
|
)
|
||||||
|
|
||||||
// WriteToStdin is a utility function that writes a string to a command's stdin.
|
// WriteToStdin is a utility function that writes a byte slice to a command's stdin.
|
||||||
func WriteToStdin(cmd *exec.Cmd, input string) error {
|
func WriteToStdin(cmd *exec.Cmd, input []byte, zeroizeInput bool) error {
|
||||||
stdin, err := cmd.StdinPipe()
|
stdin, err := cmd.StdinPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("unable to access stdin for system command: " + err.Error())
|
return errors.New("unable to access stdin for system command: " + err.Error())
|
||||||
@@ -18,16 +20,19 @@ func WriteToStdin(cmd *exec.Cmd, input string) error {
|
|||||||
defer func(stdin io.WriteCloser) {
|
defer func(stdin io.WriteCloser) {
|
||||||
_ = stdin.Close() // error ignored; if stdin could be accessed, it can probably be closed
|
_ = stdin.Close() // error ignored; if stdin could be accessed, it can probably be closed
|
||||||
}(stdin)
|
}(stdin)
|
||||||
_, _ = io.WriteString(stdin, input)
|
_, _ = stdin.Write(input)
|
||||||
|
if zeroizeInput {
|
||||||
|
security.ZeroizeBytes(input)
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadFromStdin is a utility function that reads a string from stdin.
|
// ReadFromStdin is a utility function that reads a byte slice from stdin.
|
||||||
func ReadFromStdin() string {
|
func ReadFromStdin() []byte {
|
||||||
scanner := bufio.NewScanner(os.Stdin)
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
if scanner.Scan() {
|
if scanner.Scan() {
|
||||||
return scanner.Text()
|
return scanner.Bytes()
|
||||||
}
|
}
|
||||||
return ""
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,11 +5,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"golang.org/x/term"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Input prompts the user for input and returns the input as a string.
|
// Input prompts the user for input and
|
||||||
|
// returns the input as a string.
|
||||||
func Input(prompt string) string {
|
func Input(prompt string) string {
|
||||||
fmt.Print(prompt + " ")
|
fmt.Print(prompt + " ")
|
||||||
reader := bufio.NewReader(os.Stdin)
|
reader := bufio.NewReader(os.Stdin)
|
||||||
@@ -17,15 +16,17 @@ func Input(prompt string) string {
|
|||||||
return strings.TrimRight(userInput, "\n\r ") // remove trailing newlines, carriage returns, and spaces
|
return strings.TrimRight(userInput, "\n\r ") // remove trailing newlines, carriage returns, and spaces
|
||||||
}
|
}
|
||||||
|
|
||||||
// InputHidden prompts the user for input and returns the input as a byte array, hiding the input from the terminal.
|
// Input prompts the user for input and
|
||||||
func InputHidden(prompt string) []byte {
|
// returns the input as a rune.
|
||||||
|
func InputRune(prompt string) rune {
|
||||||
fmt.Print(prompt + " ")
|
fmt.Print(prompt + " ")
|
||||||
byteInput, _ := term.ReadPassword(int(os.Stdin.Fd()))
|
reader := bufio.NewReader(os.Stdin)
|
||||||
fmt.Println()
|
userInput, _, _ := reader.ReadRune()
|
||||||
return byteInput
|
return userInput
|
||||||
}
|
}
|
||||||
|
|
||||||
// InputBinary prompts the user with a yes/no question and returns the response as a boolean.
|
// InputBinary prompts the user with a yes/no
|
||||||
|
// question and returns the response as a boolean.
|
||||||
func InputBinary(prompt string) bool {
|
func InputBinary(prompt string) bool {
|
||||||
reader := bufio.NewReader(os.Stdin)
|
reader := bufio.NewReader(os.Stdin)
|
||||||
fmt.Print(prompt + " (y/N) ")
|
fmt.Print(prompt + " (y/N) ")
|
||||||
@@ -46,13 +47,6 @@ func InputInt(prompt string, min, max int) int {
|
|||||||
if err == nil && (userInput >= min || min < 0) && (userInput <= max || max < 0) {
|
if err == nil && (userInput >= min || min < 0) && (userInput <= max || max < 0) {
|
||||||
return userInput
|
return userInput
|
||||||
}
|
}
|
||||||
|
fmt.Println()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// InputMenuGen prompts the user with a menu and returns the user's choice as an integer.
|
|
||||||
func InputMenuGen(prompt string, options []string) int {
|
|
||||||
for i, option := range options {
|
|
||||||
fmt.Printf("%d. %s\n", i+1, option)
|
|
||||||
}
|
|
||||||
return InputInt("\n"+prompt, 1, len(options))
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package front
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"slices"
|
||||||
|
|
||||||
|
"golang.org/x/term"
|
||||||
|
)
|
||||||
|
|
||||||
|
// InputMenuGen prompts the user with a menu
|
||||||
|
// and returns the user's choice as an integer.
|
||||||
|
func InputMenuGen(prompt string, options []string) int {
|
||||||
|
for i, option := range options {
|
||||||
|
fmt.Printf("%d. %s\n", i+1, option)
|
||||||
|
}
|
||||||
|
return InputInt("\n"+prompt, 1, len(options))
|
||||||
|
}
|
||||||
|
|
||||||
|
// InputMenuGenWithRuneInputs prompts the user with a menu
|
||||||
|
// and returns the user's choice as a rune.
|
||||||
|
// Ensure runeInputs contains only unique runes and matches the
|
||||||
|
// length of options.
|
||||||
|
func InputMenuGenWithRuneInputs(prompt string, options []string, runeInputs []rune) rune {
|
||||||
|
for i, option := range options {
|
||||||
|
fmt.Printf("%c. %s\n", runeInputs[i], option)
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
userInput := InputRune("\n" + prompt)
|
||||||
|
if slices.Contains(runeInputs, userInput) {
|
||||||
|
return userInput
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// InputSecret prompts the user for input and returns the
|
||||||
|
// input as a byte array, hiding the input from the terminal.
|
||||||
|
func InputSecret(prompt string) []byte {
|
||||||
|
fmt.Print(prompt + " ")
|
||||||
|
byteInput, _ := term.ReadPassword(int(os.Stdin.Fd()))
|
||||||
|
fmt.Println()
|
||||||
|
return byteInput
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
module github.com/rwinkhart/go-boilerplate
|
module github.com/rwinkhart/go-boilerplate
|
||||||
|
|
||||||
go 1.25.5
|
go 1.26.3
|
||||||
|
|
||||||
require golang.org/x/term v0.38.0
|
require golang.org/x/term v0.43.0
|
||||||
|
|
||||||
require golang.org/x/sys v0.39.0 // indirect
|
require golang.org/x/sys v0.45.0 // indirect
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
|
golang.org/x/sys v0.45.0 h1:dO4czNzziLiiXplLQgBCEpCvXQ3dnkn0SdaZSYdQ+FY=
|
||||||
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
golang.org/x/sys v0.45.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||||
golang.org/x/term v0.38.0 h1:PQ5pkm/rLO6HnxFR7N2lJHOZX6Kez5Y1gDSJla6jo7Q=
|
golang.org/x/term v0.43.0 h1:S4RLU2sB31O/NCl+zFN9Aru9A/Cq2aqKpTZJ6B+DwT4=
|
||||||
golang.org/x/term v0.38.0/go.mod h1:bSEAKrOT1W+VSu9TSCMtoGEOUcKxOKgl3LE5QEF/xVg=
|
golang.org/x/term v0.43.0/go.mod h1:lrhlHNdQJHO+1qVYiHfFKVuVioJIheAc3fBSMFYEIsk=
|
||||||
|
|||||||
@@ -1,20 +1,20 @@
|
|||||||
package stringy
|
package security
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"math/big"
|
"math/big"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// StringGen generates a random string of a specified length and complexity.
|
// BytesGen generates a random byte slice 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),
|
// Requires: complexity (minimum percentage of special characters to be returned in the generated output; set to 0 for a "simple" result),
|
||||||
// complexCharsetLevel (1 = safe for filenames, 2 = safe for most password entries, 3 = safe only for well-made password entries)
|
// 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 {
|
func BytesGen(length int, complexity float64, complexCharsetLevel uint8) []byte {
|
||||||
var actualSpecialChars int // track the number of special characters in the generated string
|
var actualSpecialChars int // track the number of special characters in the generated output
|
||||||
var minSpecialChars int // track the minimum number of special characters to accept
|
var minSpecialChars int // track the minimum number of special characters to accept
|
||||||
var extendedCharset string // additions to character set used for complex strings
|
var extendedCharset string // additions to character set used for complex outputs
|
||||||
|
|
||||||
charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" // default character set used for all strings
|
charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" // default character set used for all strings
|
||||||
const extendedCharsetFiles = "!#$%&+,-.;=@_~^()[]{}`'" // additional special characters for complex strings (safe in file names)
|
const extendedCharsetFiles = "!#$%&+,-.;=@_~^()[]{}`'" // additional special characters for complex strings (safe in file names)
|
||||||
@@ -34,34 +34,34 @@ func StringGen(length int, complexity float64, complexCharsetLevel uint8) string
|
|||||||
charset += extendedCharset
|
charset += extendedCharset
|
||||||
}
|
}
|
||||||
|
|
||||||
// loop until a string of the desired complexity is generated
|
// loop until a byte slice of the desired complexity is generated
|
||||||
for {
|
for {
|
||||||
// generate a random string
|
// generate a random output
|
||||||
result := make([]byte, length)
|
result := make([]byte, length)
|
||||||
for i := range result {
|
for i := range result {
|
||||||
val, _ := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
|
val, _ := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
|
||||||
result[i] = charset[val.Int64()]
|
result[i] = charset[val.Int64()]
|
||||||
}
|
}
|
||||||
|
|
||||||
// return early if the string is not complex
|
// return early if the desired output is not complex
|
||||||
if complexity <= 0 {
|
if complexity <= 0 {
|
||||||
return string(result)
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// count the number of special characters in the generated string
|
// count the number of special characters in the generated output
|
||||||
for _, char := range string(result) {
|
for i := range result {
|
||||||
if strings.ContainsRune(extendedCharset, char) {
|
if bytes.Contains([]byte(extendedCharset), []byte{result[i]}) {
|
||||||
actualSpecialChars++
|
actualSpecialChars++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// return the generated string if it contains enough special characters
|
// return the generated output if it contains enough special characters
|
||||||
if actualSpecialChars >= minSpecialChars {
|
if actualSpecialChars >= minSpecialChars {
|
||||||
return string(result)
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// reset special character counter
|
// reset special character counter
|
||||||
fmt.Println("Regenerating string until desired complexity is achieved...")
|
fmt.Println("Regenerating output until desired complexity is achieved...")
|
||||||
actualSpecialChars = 0
|
actualSpecialChars = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package security
|
||||||
|
|
||||||
|
// ZeroizeBytes overwrites all
|
||||||
|
// bytes in a slice with zeros.
|
||||||
|
//
|
||||||
|
//go:noinline
|
||||||
|
func ZeroizeBytes(input []byte) {
|
||||||
|
for i := range input {
|
||||||
|
input[i] = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user