Skip to content

Commit 9ae7e02

Browse files
committed
Implementing a simple login app through Console API with info security.
1 parent 8e749aa commit 9ae7e02

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

Chapter16/ConsolePwd.PNG

4.58 KB
Loading

Chapter16/Login.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.io.Console;
2+
import java.io.IOError;
3+
4+
public class Login{
5+
public static void main(String[] args) {
6+
Console console = System.console();
7+
if(console == null){
8+
System.err.println("no console device is present.");
9+
return ;
10+
}
11+
try{
12+
String username = console.readLine("Username: ");
13+
char[] pwd = console.readPassword("Password: ");
14+
// Do something useful with the username and password, this application just prints them out for demonstration.
15+
System.out.println("demo_username: " + username);
16+
System.out.println("demo_password: " + new String(pwd));
17+
// Prepare username for garbage collection.
18+
// More importantly, destroy the password by zeroing out the char array.
19+
username = "";
20+
for(int i=0;i<pwd.length;i++){
21+
pwd[i] = 0;
22+
}
23+
}catch(IOError ioe){
24+
ioe.printStackTrace();
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)