Beispiel #1
0
  /**
   * @param username ...
   * @param pw ...
   * @return ...
   */
  public boolean authenticate(String username, String pw) {
    if (this.file.exists() && (this.file.lastModified() > this.lastRead)) {
      readFile();
    } else if (!this.file.exists() && (this.users.size() > 0)) {
      this.users.clear();
    }

    String realpw = this.users.getProperty(username);

    if (realpw != null) {
      try {
        // check if password matches
        // first we try with unix crypt algorithm
        String cryptpw = Crypt.crypt(realpw, pw);

        if (realpw.equals(cryptpw)) {
          return true;
        }

        // then try MD5
        if (realpw.equals(DigestUtils.md5(pw))) {
          return true;
        }
      } catch (Exception x) {
        return false;
      }
    } else {
      if (this.parentFile != null) {
        return this.parentFile.authenticate(username, pw);
      }
    }

    return false;
  }
Beispiel #2
0
  public static void main(String[] argv) throws Exception {
    byte[] salt = new byte[5];
    SecretKeySpec skeySpec =
        new SecretKeySpec(Crypt.getRawKey("my secret", "salt".getBytes()), "AES");
    Cipher encryptCipher = Cipher.getInstance("AES");
    encryptCipher.init(Cipher.ENCRYPT_MODE, skeySpec);

    Cipher decryptCipher = Cipher.getInstance("AES");
    decryptCipher.init(Cipher.DECRYPT_MODE, skeySpec);

    testEncryptDecrypt(encryptCipher, decryptCipher, "foo", 5, Integer.MIN_VALUE);
    testEncryptDecrypt(encryptCipher, decryptCipher, "foo", Integer.MIN_VALUE, Integer.MIN_VALUE);
    testEncryptDecrypt(encryptCipher, decryptCipher, "foo", Integer.MAX_VALUE, Integer.MIN_VALUE);
    testEncryptDecrypt(encryptCipher, decryptCipher, "foo", -1, -1);
    testEncryptDecrypt(encryptCipher, decryptCipher, "foo", -1, Integer.MAX_VALUE);

    System.out.println("repeat!");
    testEncryptDecrypt(encryptCipher, decryptCipher, "foo", 5, Integer.MIN_VALUE);
    testEncryptDecrypt(encryptCipher, decryptCipher, "foo", Integer.MIN_VALUE, Integer.MIN_VALUE);
    testEncryptDecrypt(encryptCipher, decryptCipher, "foo", Integer.MAX_VALUE, Integer.MIN_VALUE);
    testEncryptDecrypt(encryptCipher, decryptCipher, "foo", -1, -1);
    testEncryptDecrypt(encryptCipher, decryptCipher, "foo", -1, Integer.MAX_VALUE);

    System.out.println("longs");

    testEncryptDecryptLong(encryptCipher, decryptCipher, "foo", 5, Long.MIN_VALUE);
    testEncryptDecryptLong(encryptCipher, decryptCipher, "foo", Long.MIN_VALUE, Long.MIN_VALUE);
    testEncryptDecryptLong(encryptCipher, decryptCipher, "foo", Long.MAX_VALUE, Long.MIN_VALUE);
    testEncryptDecryptLong(encryptCipher, decryptCipher, "foo", -1, -1);
    testEncryptDecryptLong(encryptCipher, decryptCipher, "foo", -1, Long.MAX_VALUE);

    System.out.println("repeat!");
    testEncryptDecryptLong(encryptCipher, decryptCipher, "foo", 5, Long.MIN_VALUE);
    testEncryptDecryptLong(encryptCipher, decryptCipher, "foo", Long.MIN_VALUE, Long.MIN_VALUE);
    testEncryptDecryptLong(encryptCipher, decryptCipher, "foo", Long.MAX_VALUE, Long.MIN_VALUE);
    testEncryptDecryptLong(encryptCipher, decryptCipher, "foo", -1, -1);
    testEncryptDecryptLong(encryptCipher, decryptCipher, "foo", -1, Long.MAX_VALUE);
  }