Skip to content

Instantly share code, notes, and snippets.

@drizzentic
Last active September 5, 2016 14:01
Show Gist options
  • Save drizzentic/e87eb74dee246c09217f248b62539126 to your computer and use it in GitHub Desktop.
Save drizzentic/e87eb74dee246c09217f248b62539126 to your computer and use it in GitHub Desktop.
M-Factor Authentication
import java.util.*;
import java.security.*;
import java.lang.*;
class Rand{
//We generate a pattern to be used by the client applications
//We make an assumption that the application uses a four digita pin and thus we give it an initial value of 1234
//intialize the original password
final static String originalPassword="8217";
static String enteredPassword;
public static void main(String[] args){
// the array of valid numbers
int[] numbers = {1,2,3,4};// intialize this however you want
Random rand = new Random();
//initialize the lenght of
int numLength = 4;
StringBuilder output = new StringBuilder();
for (int i = 0; i < numLength; i++) {
// use a random index to choose a number from array
int thisNum = rand.nextInt(numbers.length);
// append random number from array to output
output.append(thisNum);
}
System.out.println(output.toString());
String pattern=output.toString();
//Prompt the user to enter the password based on the given pattern
Scanner reader = new Scanner(System.in); // Reading from System.in
System.out.println("The pattern for your current login is:\n "+ pattern);
System.out.println("Enter the password that matches the pattern : ");
enteredPassword = reader.nextLine();
System.out.println("The entered passsword is " +enteredPassword+ " and original is "+originalPassword);
//invoke function to check password
//matchPassword(pattern,enteredPassword,originalPassword)
matchPassword(pattern,enteredPassword);
}
public static void matchPassword(String pattern, String enteredPassword){
//Extract the numbers from string and form the new password based on the pattern
StringBuilder sessionPass = new StringBuilder();
for(int i=0; i<pattern.length();i++){
int index=Character.getNumericValue(pattern.charAt(i));
//System.out.println(index);
char passChar=originalPassword.charAt(index);
sessionPass.append(passChar);
}
System.out.println("The session pass is "+sessionPass+ " and the entered pass is "+enteredPassword);
String sessionPassStr=sessionPass.toString();
//compare if the two passwords match and then authenticate the user
if(sessionPassStr.equals(enteredPassword)){
System.out.println("The user has been successfully authenticated");
}
else{
System.out.println("Access denied");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment