Skip to content

refactor: PasswordGen #5373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions src/main/java/com/thealgorithms/others/PasswordGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,32 @@
* @date 2017.10.25
*/
final class PasswordGen {
private static final String UPPERCASE_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final String LOWERCASE_LETTERS = "abcdefghijklmnopqrstuvwxyz";
private static final String DIGITS = "0123456789";
private static final String SPECIAL_CHARACTERS = "!@#$%^&*(){}?";
private static final String ALL_CHARACTERS = UPPERCASE_LETTERS + LOWERCASE_LETTERS + DIGITS + SPECIAL_CHARACTERS;

private PasswordGen() {
}

static String generatePassword(int minLength, int maxLength) {
Random random = new Random();

String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String lower = "abcdefghijklmnopqrstuvwxyz";
String numbers = "0123456789";
String specialChars = "!@#$%^&*(){}?";
/**
* Generates a random password with a length between minLength and maxLength.
*
* @param minLength The minimum length of the password.
* @param maxLength The maximum length of the password.
* @return A randomly generated password.
* @throws IllegalArgumentException if minLength is greater than maxLength or if either is non-positive.
*/
public static String generatePassword(int minLength, int maxLength) {
if (minLength > maxLength || minLength <= 0 || maxLength <= 0) {
throw new IllegalArgumentException("Incorrect length parameters: minLength must be <= maxLength and both must be > 0");
}

String allChars = upper + lower + numbers + specialChars;
Random random = new Random();

List<Character> letters = new ArrayList<Character>();
for (char c : allChars.toCharArray()) {
List<Character> letters = new ArrayList<>();
for (char c : ALL_CHARACTERS.toCharArray()) {
letters.add(c);
}

Expand All @@ -36,7 +47,7 @@ static String generatePassword(int minLength, int maxLength) {

// Note that size of the password is also random
for (int i = random.nextInt(maxLength - minLength) + minLength; i > 0; --i) {
password.append(letters.get(random.nextInt(letters.size())));
password.append(ALL_CHARACTERS.charAt(random.nextInt(ALL_CHARACTERS.length())));
}

return password.toString();
Expand Down
23 changes: 21 additions & 2 deletions src/test/java/com/thealgorithms/others/PasswordGenTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.thealgorithms.others;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand All @@ -10,7 +11,7 @@ public class PasswordGenTest {
@Test
public void failGenerationWithSameMinMaxLengthTest() {
int length = 10;
assertThrows(IllegalArgumentException.class, () -> { PasswordGen.generatePassword(length, length); });
assertThrows(IllegalArgumentException.class, () -> PasswordGen.generatePassword(length, length));
}

@Test
Expand All @@ -23,12 +24,30 @@ public void generateOneCharacterPassword() {
public void failGenerationWithMinLengthSmallerThanMaxLengthTest() {
int minLength = 10;
int maxLength = 5;
assertThrows(IllegalArgumentException.class, () -> { PasswordGen.generatePassword(minLength, maxLength); });
assertThrows(IllegalArgumentException.class, () -> PasswordGen.generatePassword(minLength, maxLength));
}

@Test
public void generatePasswordNonEmptyTest() {
String tempPassword = PasswordGen.generatePassword(8, 16);
assertTrue(tempPassword.length() != 0);
}

@Test
public void testGeneratePasswordWithMinGreaterThanMax() {
Exception exception = assertThrows(IllegalArgumentException.class, () -> PasswordGen.generatePassword(12, 8));
assertEquals("Incorrect length parameters: minLength must be <= maxLength and both must be > 0", exception.getMessage());
}

@Test
public void testGeneratePasswordWithNegativeLength() {
Exception exception = assertThrows(IllegalArgumentException.class, () -> PasswordGen.generatePassword(-5, 10));
assertEquals("Incorrect length parameters: minLength must be <= maxLength and both must be > 0", exception.getMessage());
}

@Test
public void testGeneratePasswordWithZeroLength() {
Exception exception = assertThrows(IllegalArgumentException.class, () -> PasswordGen.generatePassword(0, 0));
assertEquals("Incorrect length parameters: minLength must be <= maxLength and both must be > 0", exception.getMessage());
}
}