123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- package main
- import (
- "flag"
- "fmt"
- "math"
- "os"
- "strings"
- )
- func contains(arr [3]string, str string) bool {
- for _, a := range arr {
- if a == str {
- return true
- }
- }
- return false
- }
- func myUsage() {
- fmt.Println("usage: passphrase-entropy <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(" insecure Calculate entropy of an invented passphrase")
- }
- func main() {
- var charsets map[string]string
- var usedCharsets[] string
- var possibleSymbols int = 0
- var passphrase string
- var words int
- 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, "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")
- dicewareCommand := flag.NewFlagSet("diceware", flag.ExitOnError)
- passphraseFlag := dicewareCommand.String("passphrase", "", "the passphrase")
- wordsFlag := dicewareCommand.Int("words", 0, "number of words in passphrase")
- dictSizeFlag := dicewareCommand.Int("dictSize", 0, "number of words in dictionary")
- randomCommand := flag.NewFlagSet("random", flag.ExitOnError)
- insecureCommand := flag.NewFlagSet("insecure", flag.ExitOnError)
- insecureWords := insecureCommand.Int("words", 0, "number of words in invented passphrase")
- if len(os.Args) == 1 {
- myUsage()
- return
- }
- switch os.Args[1] {
- case "diceware":
- dicewareCommand.Parse(os.Args[2:])
- case "random":
- randomCommand.Parse(os.Args[2:])
- case "insecure":
- insecureCommand.Parse(os.Args[2:])
- default:
- myUsage()
- fmt.Printf("\n%q is not valid command.\n", os.Args[1])
- os.Exit(2)
- }
- if dicewareCommand.Parsed() {
- if *passphraseFlag == "" {
- dicewareCommand.Usage()
- return
- }
- if *wordsFlag == 0 {
- dicewareCommand.Usage()
- return
- }
- if *dictSizeFlag == 0 {
- dicewareCommand.Usage()
- return
- }
- entropy := math.Log2(math.Pow(float64(*dictSizeFlag), float64(*wordsFlag)))
- fmt.Println("\nPassphrase entropy:", entropy)
- }
- if randomCommand.Parsed() {
- 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)
- }
- if insecureCommand.Parsed() {
- if *insecureWords != 0 {
- insecureCommand.Usage()
- return
- }
- }
- }
|