This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Frozen23's profile picture

[Question] Password Protection

Started by Frozen23, 10 October 2012 - 07:07 PM
Frozen23 #1
Posted 10 October 2012 - 09:07 PM
Greetings, I have a code for a password protected door, the code follows:

os.PullEvent = os.PullEventRaw
while true do
term.clear()
term.setCursorPos(1, 1)
print ("Password:")
input = read ("*")
if input == "pass" then
print ("Password Correct")
rs.setOutput("left", true)
os.sleep(2)
rs.setOutput("left", false)
else
print ("Password Incorrect")
os.sleep(2)
end
end

Would there be anything I could do to shorten or improve this code?
jag #2
Posted 10 October 2012 - 09:25 PM
You could do
Spoiler

os.pullEvent = os.pullEventRaw
local password = "myPersonalPassword"
local inputPass = ""
while true do
	while inputPass ~= password do
		term.setCursorPos(1,1)
		term.clearLine()
		write("Password: ")
		inputPass = read("*")
	end
	rs.setOutput("left", true)
	os.sleep(2)
	rs.setOutput("left", false)
	inputPass = ""
end
And if you want that "Incorrect password" functionality you can just add it like this
Spoiler

os.pullEvent = os.pullEventRaw
local password = "myPersonalPassword"
local inputPass = "1983274569sdfhb3825698sdfh2544051976134985rifghs"
while true do
	while inputPass ~= password do
		if inputPass ~= "1983274569sdfhb3825698sdfh2544051976134985rifghs" then
			term.setCursorPos(1,1)
			term.clearLine()
			print("Incorrect password!")
			sleep(2)
		end
		term.setCursorPos(1,1)
		term.clearLine()
		write("Password: ")
		inputPass = read("*")
	end
	rs.setOutput("left", true)
	os.sleep(2)
	rs.setOutput("left", false)
	inputPass = "1983274569sdfhb3825698sdfh2544051976134985rifghs"
end
Why I'm setting the variable inputPass to 1983274569sdfhb3825698sdfh2544051976134985rifghs is because so that no-one will guess it, unless they're incredibly lucky.
I'm using it to check if the input is NOT equal to ↑ then print "Incorrect password!".
Ditto8353 #3
Posted 10 October 2012 - 09:35 PM
rant
There is always something you can do to improve your code, never call it 'complete', there are always more features, more robust encryption, efficiency increases…
/rant

To be fair, there isn't much you can do to improve this code. Change print("password") to write("password") is the only thing I can see and it's only a personal choice. Other than that, start sticking features into it, use bit shifting to encrypt your password and store the encryption into a file, so when you enter a password encrypt it in the same way, if it matched the stored encryption it is correct (only need it if anyone else can get access to the code). Tie passwords to user names. Lock it out after 3 failures. You get the idea…
Lyqyd #4
Posted 10 October 2012 - 11:15 PM
Adding unnecessary features is not improving code.

To OP: You've got some incorrect capitalization on that first line. The P in pullEvent (and -Raw) should be lowercase.