Random Number/Character Password Generator in Lua?

Found a bug? Just having trouble getting Trunk to do what you want? Want to help other users?

Random Number/Character Password Generator in Lua?

Postby SRhyse » Sun Aug 19, 2012 4:24 am

I'm too new to Lua to know how to pull this off, but it seems like it'd be rather basic, despite being potentially of great value.

Since you can encrypt entries in Trunk Notes, I'm looking to test out storing passwords in there. Presently, I use 1Password, and it works quite well, but it'd be nice to be able to see if the agility of Trunk Notes will make it any better for me.

The function I'm looking to use would simply generate a really long string, the length of which would possibly be defined as an argument on the function's call, of random numbers and letters, upper and lower case, and potentially punctuation. Something similar to what occurs here - http://rumkin.com/tools/password/pass_gen.php. Not that there would be a big menu and such to use it, but a simple function I could invoke to generate something like the output of that to use as passwords for things that I'd simply store in Trunk Notes and access as needed. I'd store it as a snippet that would be set to be evaluated before insertion, and vola, password generator!

Any help would be appreciated. If not, this would be great as a built in function too, though it seems basic enough to work via Lua, which I say knowing only what I've learned fiddling with the functions of other helpful Trunk Forum goers.
SRhyse
 
Posts: 95
Joined: Sat Jul 28, 2012 2:49 pm

Re: Random Number/Character Password Generator in Lua?

Postby crickladian » Wed Sep 05, 2012 7:50 pm

This persuaded me to try my first Lua script. There are almost certainly more elegant ways to do this but it may be a start if you've not cracked the problem yet.
Code: Select all
-- PassGen.lua
--
-- Lua script to create a random password string from a user-defined
-- character set. The script should be passed the length of the
-- password required.
--
-- Example: {{lua PassGen.lua, 30}}

-- Create the target character set from which random chars will be selected:

alphabetLower = "abcdefghijklmnopqrstuvwxyz"
alphabetUpper = string.upper(alphabetLower)
digits = "0123456789"
punctuation = ".,?"
charset = alphabetLower .. alphabetUpper .. digits .. punctuation
charsetLen = string.len(charset)

-- Initialise the random number generator then generate a random number that you throw
-- away. (Oddly, the first random number always seems to be the same regardless
-- of the seed - this has been noted on other Lua sites)

math.randomseed( os.time() )
dummy = math.random(charsetLen)

-- Get the required password length from the first runtime parameter:

passwordLen = args[1]

-- Generate passwordLen chars and concatenate them. (If you require the first char to be an alpha
-- character then you coud simply generate the first char from a reduced character set)

password = ""
for var = 1, passwordLen do
    index = math.random(charsetLen)
    char = string.sub(charset,index,index)
    password = password .. char
end

return(password)
crickladian
 
Posts: 6
Joined: Tue Jan 31, 2012 11:27 pm

Re: Random Number/Character Password Generator in Lua?

Postby SRhyse » Wed Sep 05, 2012 10:48 pm

Elegant or not, it works, and seems to work well! Just what I was looking for.

Hats off to you sir! Big thanks :)
SRhyse
 
Posts: 95
Joined: Sat Jul 28, 2012 2:49 pm

Re: Random Number/Character Password Generator in Lua?

Postby crickladian » Thu Sep 06, 2012 5:56 am

My pleasure. Glad it works for you!
crickladian
 
Posts: 6
Joined: Tue Jan 31, 2012 11:27 pm


Return to Problems

Who is online

Users browsing this forum: No registered users and 1 guest