-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordHasher.java
More file actions
89 lines (77 loc) · 3.01 KB
/
Copy pathPasswordHasher.java
File metadata and controls
89 lines (77 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.util.HashMap;
// Authentication layer for the inventory system
// Handles user registration and login with salted password hashing
public class PasswordHasher {
private HashMap<String, PasswordEntry> users;
private static final int HASH_ROUNDS = 5;
// Store salt and hashed password together
static class PasswordEntry {
String salt;
String hash;
PasswordEntry(String salt, String hash) {
this.salt = salt;
this.hash = hash;
}
}
public PasswordHasher() {
users = new HashMap<>();
}
// Custom hash function using polynomial rolling hash + bit mixing
// Takes input and salt, produces consistent hash output
public String hash(String input, String salt) {
String combined = input + salt;
long hashValue = 5381;
long secondary = 0;
// First pass: polynomial rolling hash (DJB2 style)
for (int i = 0; i < combined.length(); i++) {
char c = combined.charAt(i);
hashValue = ((hashValue << 5) + hashValue) ^ c;
secondary = secondary * 37 + c;
}
// Second pass: bit mixing rounds to improve distribution
for (int round = 0; round < HASH_ROUNDS; round++) {
hashValue ^= (hashValue >>> 13);
hashValue *= 0x9e3779b97f4a7c15L;
hashValue ^= (hashValue >>> 16);
secondary = secondary * 31 + (hashValue & 0xFF);
}
// Combine both values for final hash
long finalHash = hashValue ^ secondary;
return Long.toHexString(Math.abs(finalHash));
}
// Generate unique salt from system nanosecond timer
public String generateSalt() {
long timestamp = System.nanoTime();
return Long.toHexString(timestamp);
}
// Verify password by rehashing with stored salt and comparing
public boolean verify(String input, String username) {
if (!users.containsKey(username)) {
return false;
}
PasswordEntry entry = users.get(username);
String computedHash = hash(input, entry.salt);
return computedHash.equals(entry.hash);
}
// Register new user - check exists, hash password, store in HashMap
public boolean registerUser(String username, String password) {
if (users.containsKey(username)) {
System.out.println(" [ERROR] User already exists!");
return false;
}
String salt = generateSalt();
String hashedPassword = hash(password, salt);
users.put(username, new PasswordEntry(salt, hashedPassword));
System.out.println(" [SUCCESS] Account created!");
return true;
}
// Login by verifying password matches stored hash
public boolean loginUser(String username, String password) {
if (!verify(password, username)) {
System.out.println(" [ERROR] Invalid credentials!");
return false;
}
System.out.println(" [SUCCESS] Logged in as " + username);
return true;
}
}