Portfolio/java-login/Main.java
2024-02-06 22:29:40 -05:00

80 lines
2.3 KiB
Java

import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.math.BigInteger;
class Main {
public static void main(String[] args) throws NoSuchAlgorithmException {
Scanner read = new Scanner(System.in);
String Username;
String Password;
String HashedUsername;
String HashedPassword;
Creds.initTrueUsername();
Creds.initTruePassword();
System.out.println("Enter username");
Username = read.nextLine();
System.out.println("Enter password");
// this would idealy not show the password like how read -s does in shell script
Password = read.nextLine();
// Hashing the username
MessageDigest digest = MessageDigest.getInstance("SHA-512");
digest.update(Username.getBytes());
byte[] hash1 = digest.digest();
BigInteger bi1 = new BigInteger(1, hash1);
HashedUsername = bi1.toString(16);
// uncomment to echo the hash
// System.out.println(HashedUsername);
// Hashing password
digest.update(Password.getBytes());
byte[] hash2 = digest.digest();
BigInteger bi2 = new BigInteger(1, hash2);
HashedPassword = bi2.toString(16);
// System.out.println(HashedPassword);
//Username is user and Password is pass
if(HashedUsername.equals(Creds.getTrueUsername()) && HashedPassword.equals(Creds.getTruePassword())){
System.out.println("Authenticated");
}
else{
System.out.println("nope");
}
}
}
class Creds {
public static String TrueUsername;
public static String TruePassword;
public static void setTrueUsername(String value) {
TrueUsername = value;
}
public static void setTruePassword(String value) {
TruePassword = value;
}
public static String getTrueUsername() {
return TrueUsername;
}
public static String getTruePassword() {
return TruePassword;
}
// Set user and pass hash here
public static void initTrueUsername() {
setTrueUsername("b14361404c078ffd549c03db443c3fede2f3e534d73f78f77301ed97d4a436a9fd9db05ee8b325c0ad36438b43fec8510c204fc1c1edb21d0941c00e9e2c1ce2");
}
public static void initTruePassword() {
setTruePassword("5b722b307fce6c944905d132691d5e4a2214b7fe92b738920eb3fce3a90420a19511c3010a0e7712b054daef5b57bad59ecbd93b3280f210578f547f4aed4d25");
}
}