Parcourir la source

Simplify and clean up

Dennis Rodewyk il y a 5 ans
Parent
commit
3e28a8c4c2
1 fichiers modifiés avec 34 ajouts et 114 suppressions
  1. 34 114
      app.go

+ 34 - 114
app.go

@@ -20,11 +20,11 @@ func contains(arr [3]string, str string) bool {
 }
 
 func myUsage() {
-	fmt.Println("usage: passphrase-entropy <command> [<args>]")
+	fmt.Println("usage: passphrase-strength <command> [<args>]")
 	fmt.Println("Available commands are: ")
-	fmt.Println(" diceware   Calculate entropy of a diceware passphrase")
-	fmt.Println(" random     Calculate entropy of a random string")
-	fmt.Println(" invented   Calculate entropy of an invented passphrase")
+	fmt.Println(" diceware   Calculate strength of a diceware passphrase")
+	fmt.Println(" random     Calculate strength of a random string")
+	fmt.Println(" invented   Calculate strength of an invented passphrase")
 }
 
 func secondsToHuman(guesses float64) (result string) {
@@ -55,122 +55,42 @@ func secondsToHuman(guesses float64) (result string) {
 }
 
 func main() {
-	var charsets map[string]string
-	var usedCharsets[] string
-	var possibleSymbols int = 0
-
-	var haveIBeenPwned bool
-	var passphrase string
-	var pwnedFlag bool
-
+	var passphrase, words string
+	var pwnedFlag, haveIBeenPwned bool
 	var err error
 
-	charsets = make(map[string]string)
-	charsets["Numbers"] = "0123456789"
-	charsets["Lowercase"] = "abcdefghijklmnopqrstuvwxyz"
-	charsets["Uppercase"] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
-	charsets["Common Special Characters"] = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
-	charsets["Space"] = " "
-	charsets["Extended ASCII"] = "€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ"
+	flag.StringVar(&passphrase,"password", "", "the password")
+	flag.StringVar(&words, "words", "", "used words")
+	flag.BoolVar(&pwnedFlag, "pwned", false, "check if password has been seen before")
+	flag.Parse()
 
-	if len(os.Args) == 1 {
-		myUsage()
+	if len(os.Args) == 1 || passphrase == "" {
+		flag.PrintDefaults()
 		return
 	}
 
-	switch os.Args[1] {
-	case "diceware":
-		var words, dictSize int
-		dicewareCommand := flag.NewFlagSet("diceware", flag.ExitOnError)
-		dicewareCommand.IntVar(&words,"words", 0, "number of words in passphrase")
-		dicewareCommand.IntVar(&dictSize,"dictSize", 0, "number of words in dictionary")
-		dicewareCommand.Parse(os.Args[2:])
-		if dicewareCommand.Parsed() {
-			if words == 0 {
-				dicewareCommand.Usage()
-				return
-			}
-			if dictSize == 0 {
-				dicewareCommand.Usage()
-				return
-			}
-
-			entropy := math.Log2(math.Pow(float64(dictSize), float64(words)))
-			fmt.Println("\nPassphrase entropy:", entropy)
-		}
-	case "random":
-		var pwnedFlag bool
-		randomCommand := flag.NewFlagSet("random", flag.ExitOnError)
-		randomCommand.StringVar(&passphrase,"password", "", "the password")
-		randomCommand.BoolVar(&pwnedFlag, "pwned", false, "check if password has been seen before")
-		randomCommand.Parse(os.Args[2:])
-
-		if randomCommand.Parsed() {
-			if passphrase == "" {
-				randomCommand.Usage()
-				return
-			}
-			if pwnedFlag == true {
-				haveIBeenPwned, err = pwned.IsPasswordCompromised(passphrase)
-				if haveIBeenPwned == true {
-					fmt.Println("This password does not have any entropy, because it has been compromised.")
-					return
-				}
-			}
-			passphraseLength := len(passphrase)
-
-			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)
-		}
-
-	case "invented":
-		var words string
-		inventedCommand := flag.NewFlagSet("invented", flag.ExitOnError)
-		inventedCommand.StringVar(&passphrase,"password", "", "the password")
-		inventedCommand.StringVar(&words, "words", "", "used words")
-		inventedCommand.BoolVar(&pwnedFlag, "pwned", false, "check if password has been seen before")
-		inventedCommand.Parse(os.Args[2:])
-
-		if inventedCommand.Parsed() {
-
-			if pwnedFlag == true {
-				haveIBeenPwned, err = pwned.IsPasswordCompromised(passphrase)
-			}
-
-			if err != nil {
-				fmt.Println("Something went wrong.")
-			} else if haveIBeenPwned == true {
-				fmt.Println("This password does not have any entropy, because it has been compromised.")
-				return
-			} else {
-				userInputs := strings.Fields(words)
-				strength := zxcvbn.PasswordStrength(passphrase, userInputs)
-				guesses := strength.Guesses
-				fmt.Printf("\nPassword:\t\t%s\n", passphrase)
-
-				fmt.Printf("Password strength:\t%d/4\n", strength.Score)
+	if pwnedFlag == true {
+		haveIBeenPwned, err = pwned.IsPasswordCompromised(passphrase)
+	}
 
-				fmt.Printf("Guesses Log10:\t\t%f\n", math.Log10(float64(guesses)))
-				fmt.Println("\nGuess times")
-				fmt.Printf("100 / h:\t\t%s\t(throttled online attack)\n",secondsToHuman(guesses * (3600 / 100)))
-				fmt.Printf("10  / s:\t\t%s\t(unthrottled online attack)\n",secondsToHuman(guesses/10))
-				fmt.Printf("10k / s:\t\t%s\t(offline attack, slow hash, many cores)\n",secondsToHuman(guesses / 1e4))
-				fmt.Printf("10b / s:\t\t%s\t(offline attack, slow hash, many cores)\n",secondsToHuman(guesses / 1e10))
-			}
-		}
-	default:
-		myUsage()
-		fmt.Printf("\n%q is not valid command.\n", os.Args[1])
-		os.Exit(2)
+	if err != nil {
+		fmt.Println("Something went wrong.")
+	} else {
+		userInput := strings.Fields(words)
+		strength := zxcvbn.PasswordStrength(passphrase, userInput)
+		guesses := strength.Guesses
+		fmt.Printf("\nPassword:\t\t%s\n", passphrase)
+		fmt.Printf("Password strength:\t%d/4\n", strength.Score)
+
+		fmt.Printf("Guesses Log10:\t\t%f\n", math.Log10(float64(guesses)))
+		fmt.Println("\nGuess times")
+		fmt.Printf("100 / h:\t\t%s\t(throttled online attack)\n",secondsToHuman(guesses * (3600 / 100)))
+		fmt.Printf("10  / s:\t\t%s\t(unthrottled online attack)\n",secondsToHuman(guesses/10))
+		fmt.Printf("10k / s:\t\t%s\t(offline attack, slow hash, many cores)\n",secondsToHuman(guesses / 1e4))
+		fmt.Printf("10b / s:\t\t%s\t(offline attack, slow hash, many cores)\n",secondsToHuman(guesses / 1e10))
+	}
+	if haveIBeenPwned == true {
+		fmt.Println("\nThis password has been compromised and should not be used.")
+		return
 	}
 }