-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
138 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Apus - A social wall for conferences with additional features. | ||
* Copyright (C) Marcus Fihlon and the individual contributors to Apus. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package swiss.fihlon.apus.util; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
|
||
public final class PasswordUtil { | ||
|
||
private static final BCryptPasswordEncoder PASSWORD_ENCODER = new BCryptPasswordEncoder(); | ||
|
||
public static String hashPassword(@NotNull final String password) { | ||
return PASSWORD_ENCODER.encode(password); | ||
} | ||
|
||
public static boolean matches(@NotNull final String password, @NotNull final String hashedPassword) { | ||
return PASSWORD_ENCODER.matches(password, hashedPassword); | ||
} | ||
|
||
private PasswordUtil() { | ||
throw new IllegalStateException("Utility class"); | ||
} | ||
|
||
} |
68 changes: 68 additions & 0 deletions
68
src/test/java/swiss/fihlon/apus/util/PasswordUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Apus - A social wall for conferences with additional features. | ||
* Copyright (C) Marcus Fihlon and the individual contributors to Apus. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package swiss.fihlon.apus.util; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class PasswordUtilTest { | ||
|
||
@Test | ||
void positiveTests() { | ||
final String rawPassword = "password"; | ||
|
||
// let's try it five times | ||
final String hashedPassword1 = PasswordUtil.hashPassword(rawPassword); | ||
final String hashedPassword2 = PasswordUtil.hashPassword(rawPassword); | ||
final String hashedPassword3 = PasswordUtil.hashPassword(rawPassword); | ||
final String hashedPassword4 = PasswordUtil.hashPassword(rawPassword); | ||
final String hashedPassword5 = PasswordUtil.hashPassword(rawPassword); | ||
|
||
// hashing the same password multiple times produces different hashes | ||
assertNotEquals(hashedPassword1, hashedPassword2); | ||
assertNotEquals(hashedPassword1, hashedPassword3); | ||
assertNotEquals(hashedPassword1, hashedPassword4); | ||
assertNotEquals(hashedPassword1, hashedPassword5); | ||
assertNotEquals(hashedPassword2, hashedPassword1); | ||
assertNotEquals(hashedPassword2, hashedPassword3); | ||
assertNotEquals(hashedPassword2, hashedPassword4); | ||
assertNotEquals(hashedPassword2, hashedPassword5); | ||
assertNotEquals(hashedPassword3, hashedPassword1); | ||
assertNotEquals(hashedPassword3, hashedPassword2); | ||
assertNotEquals(hashedPassword3, hashedPassword4); | ||
assertNotEquals(hashedPassword3, hashedPassword5); | ||
assertNotEquals(hashedPassword4, hashedPassword1); | ||
assertNotEquals(hashedPassword4, hashedPassword2); | ||
assertNotEquals(hashedPassword4, hashedPassword3); | ||
assertNotEquals(hashedPassword4, hashedPassword5); | ||
assertNotEquals(hashedPassword5, hashedPassword1); | ||
assertNotEquals(hashedPassword5, hashedPassword2); | ||
assertNotEquals(hashedPassword5, hashedPassword3); | ||
assertNotEquals(hashedPassword5, hashedPassword4); | ||
|
||
// every hash should match the password | ||
assertTrue(PasswordUtil.matches(rawPassword, hashedPassword1)); | ||
assertTrue(PasswordUtil.matches(rawPassword, hashedPassword2)); | ||
assertTrue(PasswordUtil.matches(rawPassword, hashedPassword3)); | ||
assertTrue(PasswordUtil.matches(rawPassword, hashedPassword4)); | ||
assertTrue(PasswordUtil.matches(rawPassword, hashedPassword5)); | ||
} | ||
|
||
} |