Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ public Hash parse(final String formatted) {
String suffix = formatted.substring(MCF_PREFIX.length());
String[] parts = suffix.split("\\$");

//a valid shiro1 hash always has 4 tokens: algorithm, iterations, salt (may be empty) and digest:
if (parts.length < 4) {
throw new IllegalArgumentException("The argument is not a valid '" + ID + "' formatted hash.");
}

final String algorithmName = parts[0];
if (!new SimpleHashProvider().getImplementedAlgorithms().contains(algorithmName)) {
throw new UnsupportedOperationException("Algorithm " + algorithmName + " is not supported in shiro1 format.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.apache.shiro.crypto.SecureRandomNumberGenerator
import org.apache.shiro.crypto.hash.SimpleHash
import org.junit.jupiter.api.Test

import static org.assertj.core.api.Assertions.assertThatThrownBy
import static org.junit.jupiter.api.Assertions.*

/**
Expand Down Expand Up @@ -167,4 +168,22 @@ class Shiro1CryptFormatTest {
}
}

@Test
void testParseWithMissingTokens() {
def format = new Shiro1CryptFormat()
def malformed = [
'$shiro1$SHA-256',
'$shiro1$SHA-256$',
'$shiro1$SHA-256$abc',
'$shiro1$SHA-256$1$abc',
'$shiro1$SHA-256$1$$'
]

malformed.each { formatted ->
assertThatThrownBy { format.parse(formatted) }
.describedAs("parsing '%s'", formatted)
.isInstanceOf(IllegalArgumentException.class)
}
}

}
Loading