import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.TimeZone;
public class OTPSecurity {
public boolean CheckOTP(String input, String key) {
System.out.println("Inputted input is " + input);
System.out.println("Inputted key is " + key);
int i = 5;
while (true) {
int i2 = i - 1;
if (i <= -1) {
return false;
}
StringBuilder sb = new StringBuilder(String.valueOf(key));
sb.append(GetDateString(i2));
System.out.println("Offset is:" + i2);
System.out.println("Correct key is:" + MakeHashCode(sb.toString()));
if (input.equalsIgnoreCase(Integer.toString(MakeHashCode(sb.toString())))) {
return true;
}
i = i2;
}
}
private String GetDateString(int min) {
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
cal.add(12, min * -1);
StringBuilder sb = new StringBuilder(String.valueOf(new DecimalFormat("00").format((long) (cal.get(1) - 2000))));
sb.append(new DecimalFormat("00").format((long) (cal.get(2) + 1)));
StringBuilder sb2 = new StringBuilder(String.valueOf(sb.toString()));
sb2.append(new DecimalFormat("00").format((long) cal.get(12)));
StringBuilder sb3 = new StringBuilder(String.valueOf(sb2.toString()));
sb3.append(new DecimalFormat("00").format((long) cal.get(5)));
StringBuilder sb4 = new StringBuilder(String.valueOf(sb3.toString()));
sb4.append(new DecimalFormat("00").format((long) cal.get(11)));
return sb4.toString();
}
private int MakeHashCode(String arg) {
int nHashValue = 0;
for (int i = 0; i < arg.length(); i++) {
nHashValue = (nHashValue << 5) + nHashValue + arg.charAt(i);
}
if (nHashValue < 0) {
return nHashValue * -1;
}
return nHashValue;
}
public static void main(String[] args) {
OTPSecurity OTP = new OTPSecurity();
System.out.println("Unix Time output of input: " + OTP.MakeHashCode("yy182"));
System.out.println(OTP.CheckOTP("", "yy182"));
}
}
Compile and Execute Java Online (JDK 1.8.0)
Compile and Execute Java Online (JDK 1.8.0) helps you to Edit, Run and Share your Java Code directly from your browser. This development environment provides you version JDK 1.8.0.
How to give program Input?
The latest version of Coding Ground allows to provide program input at run time from the termnial window exactly the same way as you run your program at your own computer. So simply run a program and provide your program input (if any) from the terminal window available in the right side.
Keyboard Shortcuts
Shortcut | Description |
⌘ + Enter | Run the program |
⌘ + S | Save Project (Login Required) |
⇧ + ⌘ + S | Save As Project |
⌘ + P | New Project |
⌘ + G | Share Project |
⌘ + Z | Undo Editing |
⌘ + Y | Redo Editing |
⌘ + A | Select All Text |
⌘ + X | Cut Selected Text |
⌘ + C | Copy Selected Text |
⌘ + V | Paste Copied Text |
⌘ + F | Search Text |
⌘ + ⌥ + F | Replace Text |
Shortcut | Description |
Ctrl + Enter | Run the program |
Ctrl + S | Save Project |
Shift + Ctrl + S | Save As Project |
Ctrl + G | Share Project |
Ctrl + Z | Undo Editing |
Ctrl + Y | Redo Editing |
Ctrl + A | Select All Text |
Ctrl + X | Cut Selected Text |
Ctrl + C | Copy Selected Text |
Ctrl + V | Paste Copied Text |
Ctrl + F | Search Text |
Ctrl + H | Replace Text |
Save Java Project
You can save your Java Project with us so that you can access this project later on. To save a project you will need to create a login Id with us. So before you save a project, please create a login Id using a link given at the top right corner of this page.
Share Java Project
You can use this feature to share your Java Code with your teachers, classmates and colleagues. Just click Share Button and it will create a short link, which can be shared through Email, WhatsApp or even through Social Media. A shared link will be deleted if it has been passive for almost 3 months.