Layer Password Generator

I got tired of thinking of passwords for protected layers, so I made a generator.

Max character count is 69 and will default to this value if exceeded.
If you only want alpha numeric characters, then comment out line 2 ( min = 33, max = 41).
This will change the max character count to 60.

Characters won’t be repeated in a password.

rndSets = {
	{min = 33, max = 41},	-- non alpha numeric characters
	{min = 48, max = 57},	-- numbers
	{min = 65, max = 90},	-- uppercase
	{min = 97, max = 122},	-- lowercase
}

function getRanges()
	local ranges = {} ; ranges.total = 0
	for i, set in ipairs(rndSets) do
		local range = set.max - set.min + 1
		ranges[i] = ranges.total + 1
		ranges.total = ranges.total + range
	end
	return ranges
end

mapRanges = getRanges()

function mapChar(char)
	for ind, offset in ipairs(mapRanges) do
		if char >= offset and 
				(mapRanges[ind + 1] and 
				{char < mapRanges[ind + 1]} or 
				{char <= mapRanges.total})[1] then
			local offsetInd = (char - offset) + rndSets[ind].min
			return offsetInd
		end
	end
end

function getRndChar(word, char)
	local charMapped = mapChar(char)

	while charMapped == 34 or charMapped == 39 or word[char] do
		char = char + 1 ; char = char > mapRanges.total and 1 or char
		charMapped = mapChar(char)
	end

	word[char] = true
	return charMapped, char
end

function genPassword(length, count)
	length = length and (length >= mapRanges.total - 2 and mapRanges.total - 2 or length) or 20
	for c = 1, count or 1 do
		local password = {}
		for i = 1, length do
			local random = math.random(mapRanges.total)
			local rndCharMapped, rndChar = getRndChar(password, random)
			local nl = (count and count > 1 and c < count and i == length) and '\n' or ''
			printRaw(('%c%s'):format(rndCharMapped, nl))
		end
	end
end

Call the function like this to generate a 30 character password:

genPassword(30)

Adding a second argument will generate multiple passwords at the same time.

genPassword(30, 50)
1 Like

Now this is both cool and useful!

Wow, that’s cool. Thanks for sharing this.