Advertisement
Guest User

Untitled

a guest
Oct 10th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. import java.io.File;
  2. import java.math.BigInteger;
  3. import java.security.MessageDigest;
  4. import java.security.NoSuchAlgorithmException;
  5. import java.util.Arrays;
  6. import java.util.Scanner;
  7.  
  8. import javax.xml.parsers.DocumentBuilder;
  9. import javax.xml.parsers.DocumentBuilderFactory;
  10.  
  11. import org.w3c.dom.Document;
  12. import org.w3c.dom.Element;
  13. import org.w3c.dom.Node;
  14. import org.w3c.dom.NodeList;
  15.  
  16. public class LogInTest {
  17.  
  18.  
  19.  
  20. public static void main(String[] args) throws NoSuchAlgorithmException {
  21. // TODO Auto-generated method stub
  22. Scanner input = new Scanner (System.in);
  23.  
  24. try {
  25.  
  26. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  27. DocumentBuilder db = dbf.newDocumentBuilder();
  28. Document doc = db.parse(new File("salesperson.xml"));
  29.  
  30. NodeList salesuser = doc.getElementsByTagName("salesperson");
  31.  
  32. Element docEle = doc.getDocumentElement();
  33. NodeList nl = docEle.getChildNodes();
  34.  
  35. /*
  36. if (nl != null && nl.getLength() > 0) {
  37. for (int i = 0; i < nl.getLength(); i++) {
  38. if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
  39. Element el = (Element) nl.item(i);
  40. if (el.getNodeName().contains("staff")) {
  41. names [i] = el.getElementsByTagName("name").item(0).getTextContent();
  42. password [i] = el.getElementsByTagName("password").item(0).getTextContent();
  43.  
  44. }
  45. }
  46. }
  47. }
  48. */
  49. for (int i = 0; i < salesuser.getLength(); i++) {
  50. Node usernode = salesuser.item(i);
  51. String[] names = new String [salesuser.getLength()];
  52. String[] password = new String [salesuser.getLength()];
  53.  
  54. if(usernode.getNodeType() == Node.ELEMENT_NODE) {
  55.  
  56. Element firstElement = (Element)usernode;
  57.  
  58. NodeList nameList = firstElement.getElementsByTagName("name");
  59. Element nameElement = (Element)nameList.item(0);
  60.  
  61. NodeList textFNList = nameElement.getChildNodes();
  62. names [i] = ((Node)textFNList.item(0)).getNodeValue().trim();
  63.  
  64. NodeList passwordList = firstElement.getElementsByTagName("password");
  65. Element firstNameElement = (Element)passwordList.item(0);
  66.  
  67. NodeList textFNList1 = firstNameElement.getChildNodes();
  68. password [i] = ((Node)textFNList1.item(0)).getNodeValue().trim();
  69.  
  70. //System.out.println(names[i]+ "\t" + password[i]);
  71. }
  72. int count = 0;
  73.  
  74. while (count != 2){
  75. System.out.println("Enter username: ");
  76. String username = input.nextLine();
  77.  
  78. System.out.println("Enter password: ");
  79. String pass = input.nextLine();
  80.  
  81. count++;
  82. MessageDigest passwordhash = MessageDigest.getInstance("MD5");
  83. passwordhash.reset();
  84. passwordhash.update(pass.getBytes());
  85. byte[] digest = passwordhash.digest();
  86. BigInteger bigint = new BigInteger(1, digest);
  87. String hashed = bigint.toString(16);
  88. while (hashed.length() < 32) {
  89. hashed = "0" + hashed;
  90. }
  91.  
  92.  
  93. boolean found = Arrays.asList(names).contains(username);
  94. boolean found1 = Arrays.asList(password).contains(hashed);
  95.  
  96. if (found == true&&found1==true){
  97. System.out.println(username+" & "+hashed+" is found");
  98. break;
  99. } else {
  100. System.out.println(username+" & "+hashed+" not found");
  101. continue;
  102. }
  103. }
  104. }
  105.  
  106. }catch (Exception e){
  107. System.out.println(e.getMessage());
  108.  
  109. }
  110.  
  111.  
  112.  
  113. }
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement