Using second password

I wonder about password library.I found an example on the net.

#include <Keypad.h>
#include <Password.h>
 
String newPasswordString; //hold the new password
char newPassword[4]; //charater string of newPasswordString
 
//initialize password to 1234
//you can use password.set(newPassword) to overwrite it
Password password = Password( "1234" );
 
byte maxPasswordLength = 4; 
byte currentPasswordLength = 0;
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
 
//Define the keymap
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
 
//// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {6,7,8,9}; //connect to row pinouts
 
// Connect keypad COL0, COL1, COL2 and COL3 to these Arduino pins.
byte colPins[COLS] = {2,3,4,5}; //connect to column pinouts
 
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
 
void setup(){
   Serial.begin(9600);
}
 
void loop(){
   char key = keypad.getKey();
   if (key != NO_KEY){
      delay(60); 
      switch (key){
      case 'A': break; 
      case 'B': break; 
      case 'C': break; 
      case 'D': changePassword(); break; 
      case '#': checkPassword(); break;
      case '*': resetPassword(); break;
      default: processNumberKey(key);
      }
   }
}
 
void processNumberKey(char key) {
   Serial.print(key);
   currentPasswordLength++;
   password.append(key);
   if (currentPasswordLength == maxPasswordLength) {
      checkPassword();
   } 
}

void checkPassword() {
   if (password.evaluate()){
      Serial.println(" OK.");
   } else {
      Serial.println(" Wrong passwowrd!");
   } 
   resetPassword();
}

void resetPassword() {
   password.reset(); 
   currentPasswordLength = 0; 
}

void changePassword() {
   newPasswordString = "123";
   newPasswordString.toCharArray(newPassword, newPasswordString.length()+1); //convert string to char array
password.set(newPassword);
   resetPassword();
   Serial.print("Password changed to ");
   Serial.println(newPasswordString);
}

It works very well but just for one password.To define another one,I used

Password password = Password( "1234" );
Password password2 = Password( "1235" );

Can’t I define second password with this library?

 password.append(key);
   if (currentPasswordLength == maxPasswordLength) {
      checkPassword();
   } 

void checkPassword() {
   if (password.evaluate()){
      Serial.println(" OK.");

In this code,how does it stores the guessed password?As a variable?For example I want to print the guessed password on the serial monitor before evaluating.

Thanks

Can't I define second password with this library?

Yes. If the second one is password2, though, it seems obvious that the first one should be banana, not password. Or maybe password1.

 password.append(key);
   if (currentPasswordLength == maxPasswordLength) {
      checkPassword();
   } 

void checkPassword() {
   if (password.evaluate()){
      Serial.println(" OK.");

You have two passwords. You need to append the character to each one and check each one. That does NOT require two check functions, though.

In this code,how does it stores the guessed password?

People using the system shouldn't be guessing. Where does each digit on the password come from? You should be able to track through the code and see where that gets used. There's a clue above to help you.

For example I want to print the guessed password on the serial monitor before evaluating.

You should, obviously, print the correct passwords on a sticky note and attach it to the keypad.

Acoording to your post,I have modified the code for two passwords.It works.Here it is:

#include <Keypad.h>
#include <Password.h>
 
String newPasswordString; //hold the new password
char newPassword[4]; //charater string of newPasswordString
 
//initialize password to 1234
//you can use password.set(newPassword) to overwrite it
Password password1 = Password( "1111" );
Password password2 = Password( "2222" );
byte maxPasswordLength = 4; 
byte currentPasswordLength = 0;
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
 
//Define the keymap
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
 
//// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {2,3,4,5}; //connect to row pinouts
 
// Connect keypad COL0, COL1, COL2 and COL3 to these Arduino pins.
byte colPins[COLS] = {14,15,16}; //connect to column pinouts
 
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
 
void setup(){
   Serial.begin(9600);
}
 
void loop(){
   char key = keypad.getKey();
   if (key != NO_KEY){
      delay(60); 
      switch (key){
      case 'A': break; 
      case 'B': break; 
      case 'C': break; 
      case 'D': changePassword(); break; 
      case '#': checkPassword(); break;
      case '*': resetPassword(); break;
      default: processNumberKey(key);
      }
   }
}
 
void processNumberKey(char key) {
   Serial.print(key);
   currentPasswordLength++;
   password1.append(key);
      password2.append(key);
   if (currentPasswordLength == maxPasswordLength) {
      checkPassword();
   } 
}

void checkPassword() {
   if (password1.evaluate()){
      Serial.println(" FIRST PASSWORD IS OK ");
   } else if (password2.evaluate()){
      Serial.println(" SECOND PASSWORD IS OK ");
      changePassword();
   }
    else {
      Serial.println(" Wrong password!");
   } 
   resetPassword();
}

void resetPassword() {
   password1.reset(); 
   password2.reset(); 
   currentPasswordLength = 0; 
}

void changePassword() {
   newPasswordString = "1234";
   newPasswordString.toCharArray(newPassword, newPasswordString.length()+1); //convert string to char array
   password1.set(newPassword);
   resetPassword();
   Serial.print("Password changed to ");
   Serial.println(newPasswordString);
}

As you notice,when the second password is being entered,the first password will be set to “1234”.In serial monitor it prints “Password changed to 1234”.But when “1234” is entered ,it prints “wrong password”.Why?How does it run?Should I use EEEPROM library to store new password?

As you notice,when the second password is being entered,the first password will be set to "1234".In serial monitor it prints "Password changed to 1234".But when "1234" is entered ,it prints "wrong password".Why?How does it run?Should I use EEEPROM library to store new password?

The password library has a bug. It does NOT copy the string that you supply as the new password. It points to that string. Your string goes out of scope immediately after setting the password instance to point to it. So, you wasted your time calling the function.

You need to make the new password array global, and you need to quit pissing away resources on Strings. They contribute NOTHING to what you are doing.

You can use multiple passwords with the Arduino password library if you put all password instances into an Array and cycle through this array for each keystroke.

I wrote this simple example which sends the number of the successful password to a “server” via serial output. There you can check the number against a database and see who entered the premises and when.

/*
||  Multiple Password Entry Using Matrix Keypad with Audible Keystroke Feedback
||  Based Upon Single Password code from Nathan Sobieck: Nathan@Sobisource.com
||  wirh some inspiration from user "thereyouare"@http://forum.arduino.cc/index.php/topic,217942.0.html
||
||  update: 2014/08/21 Aldo von Wangenheim - awangenh@inf.ufsc.br
*/


//* is to validate password   
//# is to reset password attempt

/////////////////////////////////////////////////////////////////

#include <Password.h> //http://www.arduino.cc/playground/uploads/Code/Password.zip
#include <Keypad.h> //http://www.arduino.cc/playground/uploads/Code/Keypad.zip

// Create a Password Array to store multiple Passwords

const byte MAXPASSWD = 2;
Password passwordArray[MAXPASSWD] = { Password( "15973"), Password( "12345")};

const byte ROWS = 4; // Four rows
const byte COLS = 4; //  columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte rowPins[ROWS] = { 9,8,7,6 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte colPins[COLS] = { 5,4,3,2, };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
int led = 13;
int relay = 11;



// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){

  Serial.begin(9600);
  pinMode(led, OUTPUT);     
  pinMode(relay, OUTPUT);     
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
}

void loop(){
  keypad.getKey();
}

//take care of some special events
void keypadEvent(KeypadEvent eKey){
  switch (keypad.getState()){
    case PRESSED:
        /* ---- User Feedback ---- */
        tone(10,900,100); // Output a 100ms keystroke tone at 900 Hz to a speaker@pin 10
        digitalWrite(led, HIGH);  // turn the LED on 
        delay(100);               // wait for 100ms
        /* ---- Server Feedback ---- */
        Serial.print("Tecla: ");
	Serial.println(eKey);
        /* ---- Process Input ---- */
	switch (eKey){
	  case '*': checkAllPasswords(); break;  // check guessed pswd against passwordArray
	  case '#': resetAllPasswords(); break;  // clean all buffers on passwordArray
	  default: appendAllPasswords(eKey);     // update all buffers on passwordArray
         }
       digitalWrite(led, LOW);    // turn the LED off 
       noTone(10);  // stop keystroke beep
  }
}

void checkAllPasswords(){
  int i;
  for (i = 0; i < MAXPASSWD; i++) {
    if (passwordArray[i].evaluate()){
      Serial.print("Acertou Senha #");
      Serial.println(i+1);
      digitalWrite(relay, HIGH);   // turn the relay ON
      delay(2000);
      digitalWrite(relay, LOW);   // turn the LED on (HIGH is the voltage level)
      //Add code to run if it works
    }else{
      Serial.println("Wrong");
      //add code to run if it did not work
    } //endif
  } //endfor
} //end checkAllPasswords

void resetAllPasswords() {
  int i;
  for (i = 0; i < MAXPASSWD; i++) {
     passwordArray[i].reset(); 
  } //endfor
} //end resetAllPasswords

void appendAllPasswords(KeypadEvent eKey) {
  int i;
  for (i = 0; i < MAXPASSWD; i++) {
     passwordArray[i].append(eKey); 
  } //endfor
} //end resetAllPasswords

PaulS:
Yes. If the second one is password2, though, it seems obvious that the first one should be banana, not password.

“Time flies like an arrow, fruit flies like a banana.”

Groucho Marx.

I've looked at that library and its pointless - its a very complicated wrapper for strcmp(). It was written without any knowledge of any principles of computer security too. Passwords should only be stored salted and hashed.

I'm tempted to create something miles better.

Passwords should only be stored salted and hashed.

I'd agree, in general, but getting the hex file from the Arduino is not trivial. Find that it uses the password library is not necessarily trivial. Finding the bit pattern that represents the unencrypted password is not trivial.

In the end, is doing all that worth whatever the password is protecting?

Password password = Password( "123456" ); Password enrollPassword = Password( "1477" ); Password deletePassword = Password( "3699" );

//////////////////////////////////////////////////////////////////////////////////////////////////////

switch (eKey) { case '*': checkPassword(); break; case '#': password.reset(); break; default: ((enrollPassword.append(eKey))&&(deletePassword.append(eKey))&&(password.append(eKey))); //default: password.append(eKey); }

/////////////////////////////////////////////////////////////////////////////////////////////////////

void checkPassword() { if (password.evaluate()) { Serial.println("Success"); Serial.print("Password granted");

}

if (enrollPassword.evaluate()) { Serial.println("Success"); Serial.println("Enrollment Mode"); }

if (deletePassword.evaluate()) { Serial.println("Success"); Serial.println("Delete Mode"); }

else { Serial.println("Wrong"); lcd.clear(); lcd.print("Wrong Password"); } }

    default: ((enrollPassword.append(eKey))&&(deletePassword.append(eKey))&&(password.append(eKey)));

WTF? If you want to do three things with the key, do them properly:

    default:
       enrollPassword.append(eKey);
       deletePassword.append(eKey);
       password.append(eKey);
       break;