-
Notifications
You must be signed in to change notification settings - Fork 0
/
PasswordMatchTracker.java
137 lines (114 loc) · 3.84 KB
/
PasswordMatchTracker.java
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//******************************************************************************
//
// File: PasswordMatchTracker.java
// Package: ---
// Unit: Class PasswordMatchTracker
//
// A class the manages the communications between the PasswordHashGenerator class
// and UserPasswordMatcher class
//
// Author: Marko Galesic
//******************************************************************************
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.*;
public class PasswordMatchTracker
{
/**
* A private class for create password hash \ password pairs
*/
private class HashPasswordCombo
{
public byte[] hashedPassword;
public String plaintextPassword;
/**
* Constructor for HashPasswordCombo
*
* @param hashedPassword byte array containing the hash of a password
* @param database the password the hashedPassword represents
*/
public HashPasswordCombo(byte[] hashedPassword, String plaintextPassword)
{
this.hashedPassword = hashedPassword;
this.plaintextPassword = plaintextPassword;
}
}
// Hidden members
// List for storing password hash \ password pair objects
private ArrayList<HashPasswordCombo> crackedPasswords = new ArrayList<HashPasswordCombo>();
// A lock to control access priviledge for the password list
private Lock passpwordPairListReady = new ReentrantLock();
// A way to persistently signal list is populated
private Semaphore passwordPairListSemaphore = new Semaphore(1);
// Integer to keep track of the amount of hashers still running
private int numberOfPasswordHashers;
/**
* Constructor for PasswordMatchTracker - creates the object which will keep track of all
* password hash \ plaintext pairs and number of hashers running
*
* @param numberOfPasswordHashers total number of hashers
*/
public PasswordMatchTracker(int numberOfPasswordHashers)
{
this.numberOfPasswordHashers = numberOfPasswordHashers;
}
/**
* A method for getting the plaintext representation of a hashed password
*
* @param hashedPassword the hash we want to get the equivalent plaintext password for
*/
public synchronized String get(byte[] hashedPassword)
{
// Keep trying to get the password until all hashers are done
while (numberOfPasswordHashers > 0)
{
try{wait();}
catch(InterruptedException e)
{
System.err.println("Interrupted while waiting:\n" + e.getMessage());
}
// Get access priviledge
// passpwordPairListReady.lock();
String plaintextPassword = null;
// Find password if there is one
for(HashPasswordCombo hashPasswordCombo : crackedPasswords)
{
if (Arrays.equals(hashPasswordCombo.hashedPassword,hashedPassword))
{
// Read the password
plaintextPassword = hashPasswordCombo.plaintextPassword;
// Release access priviledge
//passpwordPairListReady.unlock();
return plaintextPassword;
}
}
// Release access priviledge
//passpwordPairListReady.unlock();
}
// Return null if there was no match found
return null;
}
/**
* A method for putting the plaintext representation of a hashed password into the
* password pair lists
*
* @param password the plaintext representation of a password
* @param hashedPassword the hashed representation of a password
*/
public synchronized void put(String password, byte[] hashedPassword)
{
// Aqcuire access priviledge
//passpwordPairListReady.lock();
// Create element to put into list
HashPasswordCombo combo = new HashPasswordCombo(hashedPassword, password);
// Add pair to the list
crackedPasswords.add(combo);
// Release access priviledge
//passpwordPairListReady.unlock();
// One hasher is done - decrement thread counter
numberOfPasswordHashers--;
notifyAll();
}
}