Hey! It looks quite cool, but I've noticed that there's a lot of repetition, and I rewrote the password generation logic to be simpler:
import strutils
import random
randomize()
echo "Welcome To Nim PassGen v0.3"
echo " "
# Loads the whole password dictionary in memory
var diceware = readFile("diceware.txt").splitLines()
echo "Generating Your Password..."
var lines: seq[string]
for x in 1 .. 5:
var line = rand(1 .. 7776)
lines.add diceware[line]
var passnum = rand(1 .. 1000)
let password = lines[0].toUpperAscii & lines[1 .. ^1].join("") & $passnum
echo "DONE!"
echo "Your Password is ", password
writeFile("passwd.dat", password)
The problem with your original code is that setPosition sets the position by character, not by line, so your first word won't be complete. I fixed it the simplest way - reading the whole file by in memory and splitting by lines to then get the actual random lines. Of course it's not the most efficient since you load the whole file in memory (a more efficient but a bit more verbose way would be to generate all 5 line numbers and then use the lines iterator to get them without loading the whole file in memory)
If you don't understand something from the code above - feel free to comment.
Congrats on your first program!
Important note, you are using random from Nim standard library, it is NOT a CSPRNG (Cryptographically-Secure Random Number Generator).
You should instead use nimcrypto randomBytes https://github.com/cheatfate/nimcrypto/blob/a065c174/nimcrypto/sysrand.nim#L284-L289 or nim-BearSSL BrHmacDrbgContext (declaration https://github.com/status-im/nim-bearssl/blob/ba5f4687/bearssl/decls.nim#L3665-L3701, usage https://github.com/status-im/nim-eth/blob/484fbcab/eth/keys.nim#L54-L73)
Well, lines[0] means "get the first element from the lines seq", lines[1 .. ^1] means "get elements from the first until last element" - that's nim slices, see https://nim-lang.org/docs/tut1.html#advanced-types-slices . join("") after that means - join all elements from that slice with "" (empty string) between them, so you'll get a single string from 5 different strings in a seq.
& $passnum means - concatenate the left side of the & operator with $passnum (which is - get string of the passnum int)
The stdlib cannot keep up with crypto
Anything less than 10 years old is viewed with suspicion and then you need some extra years to standardize.