Skip to content
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
11 changes: 9 additions & 2 deletions src/main/java/com/thealgorithms/conversions/Base64.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,15 @@ public static byte[] decode(String input) {

// Validate padding: '=' can only appear at the end (last 1 or 2 chars)
int firstPadding = input.indexOf('=');
if (firstPadding != -1 && firstPadding < input.length() - 2) {
throw new IllegalArgumentException("Padding '=' can only appear at the end (last 1 or 2 characters)");
if (firstPadding != -1) {
if (firstPadding < input.length() - 2) {
throw new IllegalArgumentException("Padding '=' can only appear at the end (last 1 or 2 characters)");
}
for (int i = firstPadding; i < input.length(); i++) {
if (input.charAt(i) != '=') {
throw new IllegalArgumentException("A padding '=' must not be followed by a non-padding character");
}
}
}

List<Byte> result = new ArrayList<>();
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/com/thealgorithms/conversions/Base64Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ void testInvalidPaddingPosition() {
assertThrows(IllegalArgumentException.class, () -> Base64.decode("Q=QQ"));
assertThrows(IllegalArgumentException.class, () -> Base64.decode("Q=Q="));
assertThrows(IllegalArgumentException.class, () -> Base64.decode("=QQQ"));
assertThrows(IllegalArgumentException.class, () -> Base64.decode("QQ=Q"));
assertThrows(IllegalArgumentException.class, () -> Base64.decode("AB=C"));
assertThrows(IllegalArgumentException.class, () -> Base64.decode("AB=A"));
}

@Test
Expand Down
Loading