A door with a password
An iron door that opens when you type the password on the laptop next to it. While you type, the laptop shows only stars. The password isn't written anywhere in the world: not on the sheet, not in an item. It lives in your vault, and when you leave the server, the vault forgets it, so the door stays shut until you set a new one.
Functions: SET_SECRET, HAS_SECRET, DELETE_SECRET, and INPUT with hide
What you need
- an empty sheet, a SECRET block (a CONSTANT block and an iron ingot) and two TAG blocks
- three trigger blocks with a button each: one for the door, one to set the password, one to forget it
- two laptops, a comparator and an iron door
- no rights: the door is opened by redstone, which every player may use
The idea
A password on the sheet is no secret: everybody who visits the sheet can read its LITERAL blocks. A hidden item is better, but its value still lies in the world, in a trigger block. c0de/security has a better place: your vault. Every player has one while they are online. SET_SECRET puts a value into it under a name, and a SECRET line reads it back, like a constant.
The vault has two rules that shape the door:
- Only you. A program only gets the vault of the player who started it, and only if that player owns the sheet. When somebody else presses the button, the program has no vault, and c0de/security raises VAULT_UNAVAILABLE. So the door only listens to you, and to you only with the password – like the PIN of a phone.
- Only while you are online. The vault is empty when you join. So the door needs a way to set the password (
setup), and a way to ask whether there is one: HAS_SECRET. A SECRET line for a name that isn't in the vault raises SECRET_NOT_FOUND.
Typing should be secret, too: INPUT with PARAMETER hide TRUE ; shows a * for every character, and the laptop prints the line as ***. And the tag security: "sensitive" keeps the password out of the debugging tool.
The door itself is opened by the exit code: a comparator next to the trigger block puts out a signal as strong as the exit code of the last process. EXIT 1 ; for the right password, 0 for everything else.
The program
PROGRAM passdoor
NOOP
INCLUDE c0de/security
NOOP
CONSTANT NAME STRING "doorPassword"
NOOP
FUNCTION check
RESULT right BOOLEAN
BEGIN
SECRET doorPassword STRING
VARIABLE typed STRING TAG(security: "sensitive")
CALL PRINT
PARAMETER value "Password:" ;
CALL INPUT
PARAMETER variable typed ;
PARAMETER hide TRUE ;
ASSIGN right typed = doorPassword ;
END FUNCTION
NOOP
FUNCTION setup
BEGIN
VARIABLE password STRING TAG(security: "sensitive")
VARIABLE again STRING TAG(security: "sensitive")
CALL CLS
CALL PRINT
PARAMETER value "New password:" ;
CALL INPUT
PARAMETER variable password ;
PARAMETER hide TRUE ;
CALL PRINT
PARAMETER value "Repeat it:" ;
CALL INPUT
PARAMETER variable again ;
PARAMETER hide TRUE ;
IF password = "" OR password ≠ again ;
BEGIN
CALL PRINT
PARAMETER value "Not the same twice. Nothing changed." ;
RETURN
END IF
CALL c0de/security/SET_SECRET
PARAMETER name NAME ;
PARAMETER value password ;
CALL PRINT
PARAMETER value "Password set." ;
END FUNCTION
NOOP
FUNCTION open
BEGIN
VARIABLE known BOOLEAN
VARIABLE right BOOLEAN
VARIABLE problem ERROR
CALL CLS
CALL c0de/security/HAS_SECRET problem
PARAMETER name NAME ;
RESULT result known
IF problem.code = VAULT_UNAVAILABLE ;
BEGIN
CALL PRINT
PARAMETER value "This door only listens to its owner." ;
RETURN
END IF
IF NOT known ;
BEGIN
CALL PRINT
PARAMETER value "No password yet. Run setup first." ;
RETURN
END IF
CALL check
RESULT right right
IF NOT right ;
BEGIN
CALL PRINT
PARAMETER value "Wrong password." ;
RETURN
END IF
CALL PRINT
PARAMETER value "Welcome home!" ;
EXIT 1 ;
END FUNCTION
NOOP
FUNCTION forget
BEGIN
VARIABLE problem ERROR
CALL CLS
CALL c0de/security/DELETE_SECRET problem
PARAMETER name NAME ;
IF problem.code = SECRET_NOT_FOUND ;
BEGIN
CALL PRINT
PARAMETER value "There is no password to forget." ;
RETURN
END IF
CALL PRINT
PARAMETER value "Password forgotten. The door stays shut." ;
END FUNCTIONBuilding and running it
- Put the iron door into the wall. Place the trigger block for the door next to it, with a comparator between them that points from the trigger block to the door. Put a button on the trigger block and the laptop next to it.
- Set the trigger block to the program
passdoorand the functionopen, and its Run button to Only me. Bind the laptop topassdoorandopentoo. - Inside your base, set up the second trigger block with a button for the function
setupand a second laptop bound topassdoorandsetup, and the third trigger block forforget. Give them the Run setting Only me too. - Press the button of
setup. The laptop asksNew password:; type the password and press Enter. The line shows***. Type it again afterRepeat it:. The laptop answersPassword set.

- Press the button of the door. The laptop asks
Password:. Type the password: the door opens and the laptop saysWelcome home!. A wrong password getsWrong password., and the door stays shut.

To close the door, press the button again: while a process runs, the comparator puts out 0, so the door closes while the laptop asks for the password.
Leave the server and join again, and press the button: No password yet. Run setup first. The vault forgot the password. The button of forget does the same on purpose: Password forgotten. The door stays shut.
How it works
- The name of the secret is the constant
NAME,"doorPassword". SET_SECRET, HAS_SECRET and DELETE_SECRET take it as a STRING. The SECRET line inchecknames the same secret with a LABEL,doorPassword, because a SECRET line is a declaration, like CONSTANT. setupreads the password twice with hidden INPUT, compares the two and puts it into the vault with SET_SECRET. The type of the secret is the type ofvalue, here STRING. A secondsetupreplaces the old password.openasks before it reads. HAS_SECRET answers TRUE or FALSE, and it is a CALL, so the error variableproblemcan catch VAULT_UNAVAILABLE: then a visitor pressed the button (if Run allows Players), and the door says so instead of crashing. Only when the password is there doesopencallcheck.checkreads the secret. The value is read when the cursor executes the SECRET line, socheckis a function of its own that only runs after HAS_SECRET. Its value is sensitive: the debugging tool shows***fordoorPassword.typedhas the tagsecurity: "sensitive", andpasswordandagaininsetuptoo – without it, the debugging tool would show what you typed.- Crashes open doors. A process that crashes ends with exit code 15, which also powers the door. That's why
openavoids every crash it can: it checks with HAS_SECRET first and catches VAULT_UNAVAILABLE. forgetremoves the secret with DELETE_SECRET. A secret that isn't there raises SECRET_NOT_FOUND, whichproblemcatches.
Try it yourself
- Give the visitor three tries before the door gives up.
Solution
PROGRAM passdoortries
NOOP
INCLUDE c0de/security
NOOP
CONSTANT NAME STRING "doorPassword"
NOOP
FUNCTION check
RESULT right BOOLEAN
BEGIN
SECRET doorPassword STRING
VARIABLE typed STRING TAG(security: "sensitive")
CALL PRINT
PARAMETER value "Password:" ;
CALL INPUT
PARAMETER variable typed ;
PARAMETER hide TRUE ;
ASSIGN right typed = doorPassword ;
END FUNCTION
NOOP
FUNCTION setup
BEGIN
VARIABLE password STRING TAG(security: "sensitive")
VARIABLE again STRING TAG(security: "sensitive")
CALL CLS
CALL PRINT
PARAMETER value "New password:" ;
CALL INPUT
PARAMETER variable password ;
PARAMETER hide TRUE ;
CALL PRINT
PARAMETER value "Repeat it:" ;
CALL INPUT
PARAMETER variable again ;
PARAMETER hide TRUE ;
IF password = "" OR password ≠ again ;
BEGIN
CALL PRINT
PARAMETER value "Not the same twice. Nothing changed." ;
RETURN
END IF
CALL c0de/security/SET_SECRET
PARAMETER name NAME ;
PARAMETER value password ;
CALL PRINT
PARAMETER value "Password set." ;
END FUNCTION
NOOP
FUNCTION open
BEGIN
VARIABLE known BOOLEAN
VARIABLE right BOOLEAN
VARIABLE tries INTEGER
VARIABLE problem ERROR
CALL CLS
CALL c0de/security/HAS_SECRET problem
PARAMETER name NAME ;
RESULT result known
IF problem.code = VAULT_UNAVAILABLE ;
BEGIN
CALL PRINT
PARAMETER value "This door only listens to its owner." ;
RETURN
END IF
IF NOT known ;
BEGIN
CALL PRINT
PARAMETER value "No password yet. Run setup first." ;
RETURN
END IF
REPEAT
BEGIN
CALL check
RESULT right right
ASSIGN tries tries + 1 ;
IF NOT right AND tries < 3 ;
BEGIN
CALL PRINT
PARAMETER value "Wrong password, try again." ;
END IF
END REPEAT NOT right AND tries < 3 ;
IF NOT right ;
BEGIN
CALL PRINT
PARAMETER value "Three wrong passwords. Go away!" ;
RETURN
END IF
CALL PRINT
PARAMETER value "Welcome home!" ;
EXIT 1 ;
END FUNCTION
NOOP
FUNCTION forget
BEGIN
VARIABLE problem ERROR
CALL CLS
CALL c0de/security/DELETE_SECRET problem
PARAMETER name NAME ;
IF problem.code = SECRET_NOT_FOUND ;
BEGIN
CALL PRINT
PARAMETER value "There is no password to forget." ;
RETURN
END IF
CALL PRINT
PARAMETER value "Password forgotten. The door stays shut." ;
END FUNCTIONThe REPEAT loop calls check until the password is right or tries reaches 3. Each call of check reads the secret again – that's allowed, a SECRET line can run as often as you like.
setupreplaces the password without asking. Let it ask for the old password first, if there is one – like a phone that wants the old PIN before you choose a new one.
Solution
PROGRAM passdoorchange
NOOP
INCLUDE c0de/security
NOOP
CONSTANT NAME STRING "doorPassword"
NOOP
FUNCTION check
RESULT right BOOLEAN
BEGIN
SECRET doorPassword STRING
VARIABLE typed STRING TAG(security: "sensitive")
CALL PRINT
PARAMETER value "Password:" ;
CALL INPUT
PARAMETER variable typed ;
PARAMETER hide TRUE ;
ASSIGN right typed = doorPassword ;
END FUNCTION
NOOP
FUNCTION setup
BEGIN
VARIABLE password STRING TAG(security: "sensitive")
VARIABLE again STRING TAG(security: "sensitive")
VARIABLE known BOOLEAN
VARIABLE right BOOLEAN
CALL CLS
CALL c0de/security/HAS_SECRET
PARAMETER name NAME ;
RESULT result known
IF known ;
BEGIN
CALL check
RESULT right right
IF NOT right ;
BEGIN
CALL PRINT
PARAMETER value "Wrong password. Nothing changed." ;
RETURN
END IF
END IF
CALL PRINT
PARAMETER value "New password:" ;
CALL INPUT
PARAMETER variable password ;
PARAMETER hide TRUE ;
CALL PRINT
PARAMETER value "Repeat it:" ;
CALL INPUT
PARAMETER variable again ;
PARAMETER hide TRUE ;
IF password = "" OR password ≠ again ;
BEGIN
CALL PRINT
PARAMETER value "Not the same twice. Nothing changed." ;
RETURN
END IF
CALL c0de/security/SET_SECRET
PARAMETER name NAME ;
PARAMETER value password ;
CALL PRINT
PARAMETER value "Password set." ;
END FUNCTION
NOOP
FUNCTION open
BEGIN
VARIABLE known BOOLEAN
VARIABLE right BOOLEAN
VARIABLE problem ERROR
CALL CLS
CALL c0de/security/HAS_SECRET problem
PARAMETER name NAME ;
RESULT result known
IF problem.code = VAULT_UNAVAILABLE ;
BEGIN
CALL PRINT
PARAMETER value "This door only listens to its owner." ;
RETURN
END IF
IF NOT known ;
BEGIN
CALL PRINT
PARAMETER value "No password yet. Run setup first." ;
RETURN
END IF
CALL check
RESULT right right
IF NOT right ;
BEGIN
CALL PRINT
PARAMETER value "Wrong password." ;
RETURN
END IF
CALL PRINT
PARAMETER value "Welcome home!" ;
EXIT 1 ;
END FUNCTION
NOOP
FUNCTION forget
BEGIN
VARIABLE problem ERROR
CALL CLS
CALL c0de/security/DELETE_SECRET problem
PARAMETER name NAME ;
IF problem.code = SECRET_NOT_FOUND ;
BEGIN
CALL PRINT
PARAMETER value "There is no password to forget." ;
RETURN
END IF
CALL PRINT
PARAMETER value "Password forgotten. The door stays shut." ;
END FUNCTIONsetup asks HAS_SECRET first. If there is a password, it calls check and stops after a wrong one with Wrong password. Nothing changed. Without a password, for the first setup after joining, it asks for the new one right away.
Read more
- c0de/security: SET_SECRET, HAS_SECRET, DELETE_SECRET
- c0de/security and Secrets in the vault in the guide
- SECRET and TAG in the handbook Data, Hidden items in the handbook Tools
- INPUT and the laptop
- Exit codes and Errors
- Keeping keys: secrets that stay when you leave