Be less finnicky about session key formats

This commit is contained in:
Paul Schaub 2022-11-06 20:23:01 +01:00
parent dd5d790e21
commit aa953428ee
3 changed files with 20 additions and 13 deletions

View file

@ -12,7 +12,7 @@ import sop.util.HexUtil;
public class SessionKey {
private static final Pattern PATTERN = Pattern.compile("^(\\d):([0-9a-fA-F]+)$");
private static final Pattern PATTERN = Pattern.compile("^(\\d):([0-9A-F]+)$");
private final byte algorithm;
private final byte[] sessionKey;
@ -62,6 +62,7 @@ public class SessionKey {
}
public static SessionKey fromString(String string) {
string = string.trim().toUpperCase().replace("\n", "");
Matcher matcher = PATTERN.matcher(string);
if (!matcher.matches()) {
throw new IllegalArgumentException("Provided session key does not match expected format.");

View file

@ -20,6 +20,14 @@ public class SessionKeyTest {
assertEquals(string, sessionKey.toString());
}
@Test
public void fromLowerStringTest() {
String string = "9:FCA4BEAF687F48059CACC14FB019125CD57392BAB7037C707835925CBF9F7BCD";
String lowercaseWithTrailingNewLine = "9:fca4beaf687f48059cacc14fb019125cd57392bab7037c707835925cbf9f7bcd\n";
SessionKey sessionKey = SessionKey.fromString(lowercaseWithTrailingNewLine);
assertEquals(string, sessionKey.toString());
}
@Test
public void toStringTest() {
SessionKey sessionKey = new SessionKey((byte) 9, HexUtil.hexToBytes("FCA4BEAF687F48059CACC14FB019125CD57392BAB7037C707835925CBF9F7BCD"));