passgen/main.go

102 lines
3.1 KiB
Go
Raw Normal View History

2024-03-28 13:17:50 +02:00
package main
import (
"flag"
"fmt"
"gitea.stuzer.link/stuzer05/passgen/dictionary"
"gitea.stuzer.link/stuzer05/passgen/dictionary/sets"
)
func main() {
// password flags
flagLen := flag.Int("L", 16, "length of the generated password")
flagCount := flag.Int("c", 1, "how many passwords to generate")
// set flags
flagSetAll := flag.Bool("all", false, "use all character sets")
flagSetAsciiLowercase := flag.Bool("l", false, "use lowercase ascii characters")
flagSetAsciiUppercase := flag.Bool("U", false, "use uppercase ascii characters")
flagSetNumbers := flag.Bool("n", false, "use numbers")
flagSetSpecialChars := flag.Bool("s", false, "use special characters")
flagSetSpaces := flag.Bool("S", false, "use spaces")
2024-03-28 13:28:15 +02:00
flagSetUnicodeCyrillic := flag.Bool("unicode-cyrillic", false, "use unicode cyrillic characters")
2024-03-28 13:17:50 +02:00
flagSetUnicodeJapanese := flag.Bool("unicode-japanese", false, "use unicode Japanese characters")
flagSetUnicodeChinese := flag.Bool("unicode-chinese", false, "use unicode Chinese characters")
flag.Parse()
// check password length
if *flagLen <= 0 {
fmt.Println("password length must be at least 1")
}
// check password count
if *flagCount <= 0 {
fmt.Println("passwords count must be at least 1")
}
// use all character sets
if *flagSetAll {
*flagSetAsciiLowercase = true
*flagSetAsciiUppercase = true
*flagSetNumbers = true
*flagSetSpecialChars = true
*flagSetSpaces = true
2024-03-28 13:28:15 +02:00
*flagSetUnicodeCyrillic = true
2024-03-28 13:17:50 +02:00
*flagSetUnicodeJapanese = true
*flagSetUnicodeChinese = true
}
// use default password preset no sets were requested
2024-03-28 13:28:15 +02:00
if !*flagSetAll && !*flagSetAsciiLowercase && !*flagSetAsciiUppercase && !*flagSetNumbers && !*flagSetSpecialChars && !*flagSetSpaces && !*flagSetUnicodeCyrillic && !*flagSetUnicodeJapanese && !*flagSetUnicodeChinese {
2024-03-28 13:17:50 +02:00
*flagSetAsciiLowercase = true
*flagSetAsciiUppercase = true
*flagSetNumbers = true
}
// select dictionaries
var characterSets [][]int
if *flagSetAsciiLowercase {
characterSets = append(characterSets, dictionary.ShuffleDictionarySet(sets.AsciiLowercase()))
}
if *flagSetAsciiUppercase {
characterSets = append(characterSets, dictionary.ShuffleDictionarySet(sets.AsciiUppercase()))
}
if *flagSetNumbers {
characterSets = append(characterSets, dictionary.ShuffleDictionarySet(sets.Numbers()))
}
if *flagSetSpecialChars {
characterSets = append(characterSets, dictionary.ShuffleDictionarySet(sets.SpecialChars()))
}
if *flagSetSpaces {
characterSets = append(characterSets, dictionary.ShuffleDictionarySet(sets.Space()))
}
// unicode
2024-03-28 13:28:15 +02:00
if *flagSetUnicodeCyrillic {
characterSets = append(characterSets, dictionary.ShuffleDictionarySet(sets.Cyrillic()))
}
2024-03-28 13:17:50 +02:00
if *flagSetUnicodeJapanese {
characterSets = append(characterSets, dictionary.ShuffleDictionarySet(sets.Japanese()))
}
if *flagSetUnicodeChinese {
characterSets = append(characterSets, dictionary.ShuffleDictionarySet(sets.Chinese()))
}
// generate passwords
for i := 0; i < *flagCount; i++ {
password := generatePassword(characterSets, *flagLen)
for _, i := range password {
fmt.Printf("%c", i)
}
2024-03-28 13:28:15 +02:00
// new line
if i < *flagCount {
fmt.Print("\n")
}
2024-03-28 13:17:50 +02:00
}
}