Skip to content

Commit c840fb9

Browse files
committed
Added unit tests and improved the code for testability.
1 parent 6c96f97 commit c840fb9

20 files changed

+854
-249
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# IntelliJ
2+
.idea
3+
14
# Compiled class file
25
*.class
36

.idea/workspace.xml

Lines changed: 377 additions & 210 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

HashExample.iml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,9 @@
1111
</content>
1212
<orderEntry type="inheritedJdk" />
1313
<orderEntry type="sourceFolder" forTests="false" />
14+
<orderEntry type="library" name="Maven: org.junit.jupiter:junit-jupiter-api:5.1.1" level="project" />
15+
<orderEntry type="library" name="Maven: org.apiguardian:apiguardian-api:1.0.0" level="project" />
16+
<orderEntry type="library" name="Maven: org.opentest4j:opentest4j:1.0.0" level="project" />
17+
<orderEntry type="library" name="Maven: org.junit.platform:junit-platform-commons:1.1.1" level="project" />
1418
</component>
1519
</module>

pom.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@
77
<groupId>nl.fontys.wa5</groupId>
88
<artifactId>HashExample</artifactId>
99
<version>1.0-SNAPSHOT</version>
10-
10+
<dependencies>
11+
<dependency>
12+
<groupId>org.junit.jupiter</groupId>
13+
<artifactId>junit-jupiter-api</artifactId>
14+
<version>RELEASE</version>
15+
</dependency>
16+
</dependencies>
17+
<plugin>
18+
<groupId>org.jacoco</groupId>
19+
<artifactId>jacoco-maven-plugin</artifactId>
20+
<version>0.8.2-SNAPSHOT</version>
21+
</plugin>
1122

1223
</project>

src/main/java/BLL/AccountRepository.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,23 @@ public class AccountRepository {
1616
PasswordEncrypter encrypter;
1717

1818
public AccountRepository() {
19-
encrypter = EncrypterFactory.getEncrypter(EncryptionAlgorithms.SHA2);
19+
encrypter = EncrypterFactory.getEncrypter(EncryptionAlgorithm.SHA2);
2020
}
2121

22-
public void login(String username, String passwordString) {
22+
public boolean login(String username, String passwordString) {
2323
byte[] salt = context.getSaltByUsername(username);
2424
Password password = encrypter.encrypt(passwordString, salt);
2525

2626
if (context.login(username, password)) {
2727
System.out.println("Login successful!");
28+
return true;
2829
} else {
2930
System.out.println("Login unsuccessful :(");
31+
return false;
3032
}
3133
}
3234

33-
public void register(String username, String passwordString) throws UsernameAlreadyExistsException, UsernameTooShortException, PasswordTooWeakException {
35+
public boolean register(String username, String passwordString) throws UsernameAlreadyExistsException, UsernameTooShortException, PasswordTooWeakException {
3436
if (usernameAlreadyExists(username)) {
3537
throw new UsernameAlreadyExistsException("ERROR: This username is already taken!");
3638
}
@@ -46,8 +48,10 @@ public void register(String username, String passwordString) throws UsernameAlre
4648

4749
if (context.register(username, password)) {
4850
System.out.println("Account successfully registered!");
51+
return true;
4952
} else {
5053
System.out.println("Account was not registered :(");
54+
return false;
5155
}
5256
}
5357

src/main/java/BLL/Encryption/EncrypterFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package BLL.Encryption;
22

33
public class EncrypterFactory {
4-
static public PasswordEncrypter getEncrypter(EncryptionAlgorithms algorithm) {
4+
static public PasswordEncrypter getEncrypter(EncryptionAlgorithm algorithm) {
55
switch (algorithm) {
66
default:
77
case Simple:
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package BLL.Encryption;
22

3-
public enum EncryptionAlgorithms {
3+
public enum EncryptionAlgorithm {
44
Simple,
55
SHA2
66
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package BLL.Encryption;
2+
3+
import java.security.NoSuchAlgorithmException;
4+
import java.security.SecureRandom;
5+
import java.util.Arrays;
6+
7+
public class Salt {
8+
private final int SALT_SIZE = 32;
9+
private byte[] salt;
10+
11+
public Salt() {
12+
salt = new byte[SALT_SIZE];
13+
try {
14+
SecureRandom.getInstanceStrong().nextBytes(salt);
15+
} catch (NoSuchAlgorithmException e) {
16+
throw new RuntimeException(e);
17+
}
18+
}
19+
20+
public byte[] get() {
21+
return salt;
22+
}
23+
24+
@Override
25+
public boolean equals(Object o) {
26+
if (o == null || !(o instanceof Salt)) {
27+
return false;
28+
}
29+
30+
Salt s = (Salt)o;
31+
return Arrays.equals(this.salt, s.get());
32+
}
33+
}

src/main/java/BLL/Encryption/Sha2Encrypter.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,20 @@
44
import javax.crypto.SecretKeyFactory;
55
import javax.crypto.spec.PBEKeySpec;
66
import java.security.NoSuchAlgorithmException;
7-
import java.security.SecureRandom;
87
import java.security.spec.InvalidKeySpecException;
98

109
public class Sha2Encrypter implements PasswordEncrypter {
1110
private final int ITERATIONS = 10000;
1211
private final int KEYLENGTH = 256;
1312
private final int SALT_SIZE = 32;
1413

15-
1614
@Override
1715
public Sha2Password encrypt(String password) {
1816
return this.encrypt(password, null);
1917
}
2018

2119
public Sha2Password encrypt(String password, byte[] inputSalt) {
22-
byte[] salt = inputSalt == null ? generateSalt() : inputSalt;
20+
byte[] salt = inputSalt == null ? (new Salt()).get() : inputSalt;
2321

2422
try {
2523
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512");
@@ -28,14 +26,8 @@ public Sha2Password encrypt(String password, byte[] inputSalt) {
2826
byte[] hash = key.getEncoded();
2927

3028
return new Sha2Password(hash, salt, ITERATIONS, KEYLENGTH);
31-
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
29+
} catch (NoSuchAlgorithmException | InvalidKeySpecException | IllegalArgumentException e) {
3230
throw new RuntimeException(e);
3331
}
3432
}
35-
36-
private byte[] generateSalt() {
37-
byte[] salt = new byte[SALT_SIZE];
38-
(new SecureRandom()).nextBytes(salt);
39-
return salt;
40-
}
4133
}
Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package BLL.Encryption;
22

3-
import java.security.NoSuchAlgorithmException;
4-
import java.security.SecureRandom;
5-
63
public class SimpleEncrypter implements PasswordEncrypter {
7-
private final int SALT_SIZE = 32;
84

95
@Override
106
public SimplePassword encrypt(String password) {
@@ -13,7 +9,7 @@ public SimplePassword encrypt(String password) {
139

1410
@Override
1511
public SimplePassword encrypt(String password, byte[] inputSalt) {
16-
byte[] salt = inputSalt == null ? generateSalt() : inputSalt;
12+
byte[] salt = inputSalt == null ? (new Salt()).get() : inputSalt;
1713
byte[] encryptedPass = new byte[password.length()];
1814

1915
byte s = 0;
@@ -27,15 +23,4 @@ public SimplePassword encrypt(String password, byte[] inputSalt) {
2723

2824
return new SimplePassword(new String(encryptedPass), salt);
2925
}
30-
31-
private byte[] generateSalt() {
32-
byte[] salt = new byte[SALT_SIZE];
33-
try {
34-
SecureRandom.getInstanceStrong().nextBytes(salt);
35-
} catch (NoSuchAlgorithmException e) {
36-
throw new RuntimeException(e);
37-
}
38-
39-
return salt;
40-
}
4126
}

0 commit comments

Comments
 (0)