Skip to content

Commit

Permalink
Implemented a helper flag in both commands to spit out a handful of k…
Browse files Browse the repository at this point in the history
…eys in hex and base64 with a variety of lengths.
  • Loading branch information
vaughany committed Sep 10, 2021
1 parent a7911d5 commit b691f09
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cmd/collector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ func main() {

var (
version = false
keyGen = false
debug = false
testing = false
receiverURL = "http://127.0.0.1:8080"
logName = vcms.CreateLogName(vcms.LogFolder, cmdCodename)
)

flag.BoolVar(&debug, "d", debug, "Shows debugging info")
flag.BoolVar(&keyGen, "k", false, "Quickly generate a few random keys")
flag.BoolVar(&testing, "t", testing, "Creates a random hostname, username and IP address")
flag.StringVar(&receiverURL, "r", receiverURL, "URL of the 'Receiver' application")
flag.BoolVar(&version, "v", version, "Show version info and quit")
Expand All @@ -50,6 +52,11 @@ func main() {
os.Exit(0)
}

if keyGen {
vcms.ShowRandomKeys()
os.Exit(0)
}

// Opens a logfile for writing and also outputs to stdout.
vcms.CheckLogFolder(vcms.LogFolder)
lf, err := os.OpenFile(logName, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0755)
Expand Down
7 changes: 7 additions & 0 deletions cmd/receiver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@ func main() {

var (
version = false
keyGen = false
receiverURL = "127.0.0.1:8080" // Don't put e.g. http:// at the start. Add this to docs.
logName = vcms.CreateLogName(vcms.LogFolder, cmdCodename)
)

flag.BoolVar(&debug, "d", debug, "Shows debugging info")
flag.BoolVar(&version, "v", false, "Show version info and quit")
flag.BoolVar(&keyGen, "k", false, "Quickly generate a few random keys")
flag.StringVar(&receiverURL, "r", receiverURL, "URL to run this application's web server on")
flag.Parse()

Expand All @@ -90,6 +92,11 @@ func main() {
os.Exit(0)
}

if keyGen {
vcms.ShowRandomKeys()
os.Exit(0)
}

// Opens a logfile for writing and also outputs to stdout.
vcms.CheckLogFolder(vcms.LogFolder)
lf, err := os.OpenFile(logName, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0755)
Expand Down
40 changes: 40 additions & 0 deletions internal/vcms.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
package vcms

import (
"encoding/base64"
"encoding/hex"
"fmt"
"log"
"math/rand"
"os"
"runtime"
"strings"
"time"
)

Expand Down Expand Up @@ -132,3 +136,39 @@ func CreateLogName(logFolder string, cmdName string) string {

return fmt.Sprintf("%s/%s_%s.log", logFolder, cmdName, time)
}

// Creates a random string of length n, in either hex or base64.
func createRandomString(n int, keyType string) string {
b := make([]byte, (n+1)/2)

src := rand.New(rand.NewSource(time.Now().UnixNano()))
if _, err := src.Read(b); err != nil {
panic(err)
}

switch keyType {
case "hex":
return hex.EncodeToString(b)
case "base64":
return base64.URLEncoding.EncodeToString(b)
default:
return ""
}
}

// ShowRandomKeys prints a bunch of strings of varying lengths, generated from random data and converted to hex or base64.
func ShowRandomKeys() {
fmt.Println("Randonly generated bytes, converted to hex and base64 strings, suitable for being used as a pre-shared key.")
fmt.Println("Re-run for more random keys.")
fmt.Println("\nHex:")
for _, n := range []int{16, 32, 48, 64, 96, 128} {
key := createRandomString(n, "hex")
fmt.Printf("%3d chars: %s / %s\n", len(key), key, strings.ToUpper(key))
}

fmt.Println("\nBase64:")
for _, n := range []int{24, 48, 72, 96, 144, 192} {
key := createRandomString(n, "base64")
fmt.Printf("%3d chars: %s\n", len(key), key)
}
}

0 comments on commit b691f09

Please sign in to comment.