Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.math.BigInteger;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import java.util.Arrays;
- import java.util.Scanner;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- public class LogInTest {
- public static void main(String[] args) throws NoSuchAlgorithmException {
- // TODO Auto-generated method stub
- Scanner input = new Scanner (System.in);
- try {
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- DocumentBuilder db = dbf.newDocumentBuilder();
- Document doc = db.parse(new File("salesperson.xml"));
- NodeList salesuser = doc.getElementsByTagName("salesperson");
- Element docEle = doc.getDocumentElement();
- NodeList nl = docEle.getChildNodes();
- /*
- if (nl != null && nl.getLength() > 0) {
- for (int i = 0; i < nl.getLength(); i++) {
- if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
- Element el = (Element) nl.item(i);
- if (el.getNodeName().contains("staff")) {
- names [i] = el.getElementsByTagName("name").item(0).getTextContent();
- password [i] = el.getElementsByTagName("password").item(0).getTextContent();
- }
- }
- }
- }
- */
- for (int i = 0; i < salesuser.getLength(); i++) {
- Node usernode = salesuser.item(i);
- String[] names = new String [salesuser.getLength()];
- String[] password = new String [salesuser.getLength()];
- if(usernode.getNodeType() == Node.ELEMENT_NODE) {
- Element firstElement = (Element)usernode;
- NodeList nameList = firstElement.getElementsByTagName("name");
- Element nameElement = (Element)nameList.item(0);
- NodeList textFNList = nameElement.getChildNodes();
- names [i] = ((Node)textFNList.item(0)).getNodeValue().trim();
- NodeList passwordList = firstElement.getElementsByTagName("password");
- Element firstNameElement = (Element)passwordList.item(0);
- NodeList textFNList1 = firstNameElement.getChildNodes();
- password [i] = ((Node)textFNList1.item(0)).getNodeValue().trim();
- //System.out.println(names[i]+ "\t" + password[i]);
- }
- int count = 0;
- while (count != 2){
- System.out.println("Enter username: ");
- String username = input.nextLine();
- System.out.println("Enter password: ");
- String pass = input.nextLine();
- count++;
- MessageDigest passwordhash = MessageDigest.getInstance("MD5");
- passwordhash.reset();
- passwordhash.update(pass.getBytes());
- byte[] digest = passwordhash.digest();
- BigInteger bigint = new BigInteger(1, digest);
- String hashed = bigint.toString(16);
- while (hashed.length() < 32) {
- hashed = "0" + hashed;
- }
- boolean found = Arrays.asList(names).contains(username);
- boolean found1 = Arrays.asList(password).contains(hashed);
- if (found == true&&found1==true){
- System.out.println(username+" & "+hashed+" is found");
- break;
- } else {
- System.out.println(username+" & "+hashed+" not found");
- continue;
- }
- }
- }
- }catch (Exception e){
- System.out.println(e.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement