コード例 #1
0
ファイル: PasswordHash.java プロジェクト: arpitpanwar/Crawler
  /**
   * Tests the basic functionality of the PasswordHash class
   *
   * @param args ignored
   */
  public static void main(String[] args) {
    try {
      // Print out 10 hashes
      for (int i = 0; i < 10; i++) System.out.println(PasswordHash.createHash("p\r\nassw0Rd!"));

      // Test password validation
      boolean failure = false;
      System.out.println("Running tests...");
      for (int i = 0; i < 100; i++) {
        String password = "" + i;
        String hash = createHash(password);
        String secondHash = createHash(password);
        if (hash.equals(secondHash)) {
          System.out.println("FAILURE: TWO HASHES ARE EQUAL!");
          failure = true;
        }
        String wrongPassword = "" + (i + 1);
        if (validatePassword(wrongPassword, hash)) {
          System.out.println("FAILURE: WRONG PASSWORD ACCEPTED!");
          failure = true;
        }
        if (!validatePassword(password, hash)) {
          System.out.println("FAILURE: GOOD PASSWORD NOT ACCEPTED!");
          failure = true;
        }
      }
      if (failure) System.out.println("TESTS FAILED!");
      else System.out.println("TESTS PASSED!");
    } catch (Exception ex) {
      System.out.println("ERROR: " + ex);
    }
  }
コード例 #2
0
  private void handleRequest(HttpServletRequest request, HttpServletResponse response) {

    EntityStore store =
        new DBWrapper(getServletContext().getInitParameter(Constants.SERVLET_STORE_PARAM))
            .getStore();
    try {
      String firstName = request.getParameter("firstname");
      String lastName = request.getParameter("lastname");
      String email = request.getParameter("email");
      String password = request.getParameter("password");

      if (firstName == null || lastName == null || email == null || password == null) {
        response.sendRedirect("register.html#error");
      }

      if (WebUiUtils.isEmailPresent(email, store)) {
        response.sendRedirect("register.html#uniqueError");
      } else {
        UserEntity user = new UserEntity();
        user.setUserId(Utils.generateUniqueId());
        user.setUserEmail(email);
        user.setUserFirstName(firstName);
        user.setUserLastName(lastName);
        user.setUserPassword(PasswordHash.createHash(password));
        user.setUserType(Constants.USER_TYPE_GENERAL);
        user.setLastLogin(0);
        UserAccessor accessor = new UserAccessor(store);
        accessor.putEntity(user);
        response.sendRedirect("register.html#success");
      }

    } catch (IOException ioe) {
      LOG.debug("Unable to send register response: " + ioe.getMessage());
    } catch (Exception e) {
      LOG.debug("Unexpected error during request processing: " + e.getMessage());
    }
  }