Skip to content

Instantly share code, notes, and snippets.

@nanoninja
Created February 10, 2015 15:11
Show Gist options
  • Save nanoninja/39a59027da396bb7c46d to your computer and use it in GitHub Desktop.
Save nanoninja/39a59027da396bb7c46d to your computer and use it in GitHub Desktop.
KeyGenGo
// https://play.golang.org/p/zu7mtEWy7e
package main
import (
"flag"
"fmt"
"log"
"math/rand"
"os"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
flag.Usage = func() {
fmt.Printf("Usage: kgen [options] param\n")
flag.PrintDefaults()
}
}
const alpha = "abcdefghijklmnopqrstuvwxyz"
const alphaCap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
const numeric = "0123456789"
var max = flag.Uint("max", 255, "Max size")
var format = flag.String("type", "all", "Type of key (all,upper,lower,num)")
var size = flag.Uint("len", 12, "Length of key")
func Random(c chan string) {
chars := getChars(*format)
for {
c <- string(chars[rand.Intn(len(chars))])
}
}
func getChars(key string) string {
m := map[string]string{
"all": numeric + alpha + alphaCap,
"upper": alphaCap,
"lower": alpha,
"num": numeric,
}
if s, ok := m[key]; ok {
return s
}
return m["all"]
}
func printer(size uint, c chan string) {
var i uint
for i = 0; i < size; i++ {
fmt.Printf("%s", <-c)
}
fmt.Println("")
}
func keygen(length uint, max uint) {
ch := make(chan string)
go Random(ch)
if length > max {
log.Printf("The size is more than %d", max)
os.Exit(1)
}
printer(length, ch)
}
func main() {
flag.Parse()
keygen(*size, *max)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment