Browse Source

Introduce -random, -insecure, -diceware flags

Dennis Rodewyk 5 years ago
parent
commit
34fd3c9c23
1 changed files with 42 additions and 19 deletions
  1. 42 19
      app.go

+ 42 - 19
app.go

@@ -1,6 +1,7 @@
 package main
 package main
 
 
 import (
 import (
+	"flag"
 	"fmt"
 	"fmt"
 	"math"
 	"math"
 	"os"
 	"os"
@@ -22,33 +23,55 @@ func main() {
 	var usedCharsets[] string
 	var usedCharsets[] string
 	var possibleSymbols int = 0
 	var possibleSymbols int = 0
 
 
-	if len(os.Args) == 1 {
-			fmt.Println("Usage:\n\tpassphrase-entropy passphrase\n\nNo passphrase given.")
-			os.Exit(0)
-	}
-
-	passphrase := strings.Join(os.Args[1:]," ")
-	passphraseLength := len(passphrase)
-
-	fmt.Println("\nEvaluated string:", passphrase)
+	var passphrase string
+	var words int
 
 
 	charsets = make(map[string]string)
 	charsets = make(map[string]string)
 	charsets["Numbers"] = "0123456789"
 	charsets["Numbers"] = "0123456789"
 	charsets["Lowercase"] = "abcdefghijklmnopqrstuvwxyz"
 	charsets["Lowercase"] = "abcdefghijklmnopqrstuvwxyz"
 	charsets["Uppercase"] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 	charsets["Uppercase"] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
-	charsets["Common Special Characters"] = " !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~‚"
+	charsets["Common Special Characters"] = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
+	charsets["Space"] = " "
 	charsets["Extended ASCII"] = "€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ"
 	charsets["Extended ASCII"] = "€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ"
 
 
-	for key, value := range charsets {
-		if strings.ContainsAny(passphrase, value) {
-			possibleSymbols += len(value)
-			usedCharsets = append(usedCharsets, key)
-		}
+	flag.StringVar(&passphrase, "diceware", "", "a diceware passphrase")
+	flag.IntVar(&words, "words", 0, "How many words")
+	flag.StringVar(&passphrase, "random", "", "a random string password")
+	flag.StringVar(&passphrase, "insecure", "", "a human invented password")
+
+	flag.Parse()
+
+	seen := make(map[string]bool)
+	flag.Visit(func(f *flag.Flag) { seen[f.Name] = true })
+
+	if len(seen) == 0 {
+		flag.PrintDefaults()
+		os.Exit(2) // the same exit code flag.Parse uses
 	}
 	}
 
 
-	entropy := math.Log2(math.Pow(float64(possibleSymbols), float64(passphraseLength)))
+	fmt.Println("\nEvaluated string:", passphrase)
+
+	if dice := seen["diceware"]; dice {
+		if dice := !seen["words"]; dice {
+			os.Exit(2)
+		} else {
+			entropy := math.Log2(math.Pow(float64(7776), float64(words)))
+			fmt.Println("\nPassphrase entropy:", entropy)
+		}
+	} else if rnd := seen["random"]; rnd {
+		passphraseLength := len(passphrase)
 
 
-	fmt.Println("\nNumber of characters:", passphraseLength)
-	fmt.Println("\nUsed character sets:", strings.Join(usedCharsets, ", "))
-	fmt.Println("\nPassphrase entropy:", entropy)
+		for key, value := range charsets {
+			if strings.ContainsAny(passphrase, value) {
+				possibleSymbols += len(value)
+				usedCharsets = append(usedCharsets, key)
+			}
+		}
+
+		entropy := math.Log2(math.Pow(float64(possibleSymbols), float64(passphraseLength)))
+
+		fmt.Println("\nNumber of characters:", passphraseLength)
+		fmt.Println("\nUsed character sets:", strings.Join(usedCharsets, ", "))
+		fmt.Println("\nPassphrase entropy:", entropy)
+	}
 }
 }