Introduction to Java syntax
Objectives
In this first tutorial, we’re simply going to familiarize ourselves with Java syntax, without using object-oriented programming.
This is why methods are defined as static. They are class methods. You don’t need to instantiate an object to call them.
We’re going to do a few exercises on the subject of passwords.
1 Introduction
In this tutorial, we will use the INSEE SSPCloud Datalab.
1.1 Git setup
On the datalab, your services have a limited lifespan.
To save your programs, the best practice is to use a git repository. We will therefore create and use a token to communicate with GitHub.
To follow the process, you need to have a GitHub account. It is also possible to follow a similar process with GitLab.
Generate a GitHub Token
If you have already generated and declared a GitHub token, there’s no need to repeat these 2 steps.
-
- Fill in:
- token name: Datalab GENES
- expiration date ➡️ Custom ➡️ 1 year
- ✅ Check the repo box
- Click on
- Copy the token starting with
ghp_
and keep it safe for a few minutes
- Fill in:
- This token will only be visible once
- If you lose it or if it expires, you must generate a new one
Declare Your Token
GitHub has provided you with a token. Now, you need to declare it on the Datalab:
-
- Git username
- email (the one used for your GitHub account)
You can now exchange code between the Datalab services and your GitHub repositories. 🎉
1.2 Launch Vscode Java
A service is available with Visual Studio Code and Java.
You won’t find this service in the basic catalog. You have to use the link above to launch it.
It uses a specific Docker image
-
- enter :
/home/onyxia/work/ENSAI-2A-Java-TP/
➡️ OK
- enter :
- SHIFT + ALT + A: Comment / Uncomment selected lines
- SHIFT + ALT + O: Auto import
2 Exercices
-
- Terminal > New Terminal (or CTRL + ù)
-
- if you need help, check out Unix basic commands
Compile
- a single Java file:
javac MyClass.java
- all Java files:
javac *.java
- specify output directory for .class files (recommended):
javac -d bin MyClass.java
Run
To run a class, it must have a main() method.
java MyClass
- when .class files are in the bin/ directory:
java -cp bin MyClass
As mentioned at the beginning, the aim of this tutorial is not OOP, but simply basic Java syntax.
So all the methods defined here are static ➡️ They are class methods ➡️ They can be called without instantiating an object.
2.1 Brute force
You will write a Java method that attempts to brute-force a hashed 6-digit numeric password.
- The method will iterate through all numbers from 000000 to 999999
- For each number, Generate and compare hash with the given target hash
- If a match is found, return the original number else return null
- Use method hashPassword()
String.format("%06d", 123)
➡️ “000123”
2.2 Strong password
You will write a Java method to check if a password is strong. A password is strong if:
- ✅ It has at least 12 characters
- ✅ It contains at least one uppercase letter
- ✅ It contains at least one lowercase letter
- ✅ It contains at least one digit
- ❌ It does not contain whitespace
Usefull methods:
Character.isUpperCase(<char>)
Character.isDigit(<char>)
Character.isWhitespace(<char>)
2.3 Check password list
You will write a method that takes a list of passwords (ArrayList
The method should return a HashMap where:
- The keys are the passwords
- The values are true if the password is strong, false otherwise
2.4 Generate a Random Password
Objective: Implement a method to generate a secure random password that includes at least:
- ✅ 1 uppercase letter
- ✅ 1 lowercase letter
- ✅ 1 digit
- ✅ 1 special character
- Ensure length is at least 4
- Create a list of characters
- Add at least one character from each group
- Fill the remaining characters randomly from all groups
- Shuffle the password to mix character types
Collections.shuffle(<myList>)
- Convert the list into a String and return it
SecureRandom random = new SecureRandom();
System.out.println(random.nextInt(21));
2.5 Login System
Complete file Login.java:
Bonus:
- After entering username, give 3 chances to input the correct password
- If all 3 attempts fail, restart username input
Scanner scanner = new Scanner(System.in);
System.out.print("Enter username: ");
String username = scanner.nextLine();
.close(); scanner