Created
June 16, 2023 11:05
-
-
Save vdvm/1ce727a9ac6cfdf96cbab0ae2f753b36 to your computer and use it in GitHub Desktop.
Diceware Password Generator by ChatGPT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"flag" | |
"fmt" | |
"log" | |
"math/rand" | |
"os" | |
"strings" | |
"time" | |
) | |
var dicewareList []string | |
var separatorChars = []rune("[email protected]_*") | |
func main() { | |
rand.Seed(time.Now().UnixNano()) | |
filename := flag.String("file", "diceware_nl.txt", "Bestandsnaam van de diceware woordenlijst") | |
length := flag.Int("length", 5, "Aantal woorden in het gegenereerde wachtwoord") | |
flag.Parse() | |
err := loadDicewareList(*filename) | |
if err != nil { | |
log.Fatalf("Fout bij het laden van diceware woordenlijst: %v", err) | |
} | |
password := generatePassword(*length) | |
fmt.Println(password) | |
} | |
func loadDicewareList(filename string) error { | |
file, err := os.Open(filename) | |
if err != nil { | |
return err | |
} | |
defer file.Close() | |
scanner := bufio.NewScanner(file) | |
for scanner.Scan() { | |
dicewareList = append(dicewareList, scanner.Text()) | |
} | |
if err := scanner.Err(); err != nil { | |
return err | |
} | |
return nil | |
} | |
func generatePassword(length int) string { | |
var passwordParts []string | |
uppercaseIndex := rand.Intn(length) | |
for i := 0; i < length; i++ { | |
word := dicewareList[rand.Intn(len(dicewareList))] | |
if i == uppercaseIndex { | |
word = strings.ToUpper(word) | |
} | |
passwordParts = append(passwordParts, word) | |
if i < length-1 { | |
separator := string(separatorChars[rand.Intn(len(separatorChars))]) | |
passwordParts = append(passwordParts, separator) | |
} | |
} | |
return strings.Join(passwordParts, "") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment