这是课程:
import at.favre.lib.crypto.bcrypt.BCrypt;
import com.jsoniter.JsonIterator;
import com.jsoniter.any.Any;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import static java.util.Map.entry;
public class Helper {
public static class ConnectionPath {
String[] pathParts;
Map<String, String> params;
public ConnectionPath (String[] pathParts, Map<String, String> params) {
this.pathParts = pathParts;
this.params = params;
}
}
public static class MessagePacket {
final String hash;
final String[] intention;
final String preData;
Object postData;
public MessagePacket (String[] intention, String controlSum, String data) {
this.intention = intention;
this.hash = controlSum;
this.preData = data;
}
public void parseData (Class pattern) {
postData = JsonIterator.deserialize(preData, pattern);
}
}
@Contract("_ -> new")
public static @NotNull ConnectionPath parseURI (@NotNull String uri) {
int index = uri.indexOf('?');
String path = uri.substring(0, index);
String query = uri.substring(index + 1);
String[] pathParts = path.split("/");
Map<String, String> params = new HashMap<>();
for (String param : query.split("&")) {
String[] entry = param.split("=");
params.put(entry[0], entry[1]);
}
return new ConnectionPath(pathParts, params);
}
@Contract("_ -> new")
public static @NotNull MessagePacket parsePacket (@NotNull String packet) {
String[] splitPacket = packet.split("%");
return new MessagePacket(splitPacket[0].split("-"), splitPacket[1], splitPacket[2]);
}
public static Boolean verifierBCrypt (@NotNull String data, byte[] hash_data) {
return BCrypt.verifyer().verify(Arrays.copyOfRange(data.toCharArray(), 0, Math.min(data.toCharArray().length, 72)), hash_data).verified;
}
public static String SHA512 (String string) {
MessageDigest md;
try {
md = MessageDigest.getInstance("SHA-512");
} catch (NoSuchAlgorithmException _) {
return string;
}
byte[] bytes = md.digest(string.getBytes(StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
for (byte aByte : bytes) {
sb.append(Integer.toString((aByte & 0xff) + 0x100, 16).substring(1));
}
return sb.toString();
}
}
我正在测试:
public class Starter {
public static void main (String[] args) {
Helper.parseURI("/id=4&token=user.4.wsm3mOlDwbhaHD0iG33nQ9CwBEIf3B7al5Bylki1XpuKOHAxy2og1mmqdIs5yALmW");
System.out.println("Прошло!");
}
}
但程序崩溃了:
C:\Users\Fluorum\.jdks\sapmachine-21\bin\java.exe --enable-preview "-javaagent:C:\Users\Fluorum\AppData\Local\Programs\IntelliJ IDEA Community Edition\lib\idea_rt.jar=56493:C:\Users\Fluorum\AppData\Local\Programs\IntelliJ IDEA Community Edition\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath E:\Projects\GigaChat\RTCD\out\production\RTCD;C:\Users\Fluorum\.m2\repository\com\jsoniter\jsoniter\0.9.23\jsoniter-0.9.23.jar;C:\Users\Fluorum\.m2\repository\org\java-websocket\Java-WebSocket\1.5.4\Java-WebSocket-1.5.4.jar;C:\Users\Fluorum\.m2\repository\org\slf4j\slf4j-api\2.0.6\slf4j-api-2.0.6.jar;C:\Users\Fluorum\.m2\repository\at\favre\lib\bcrypt\0.10.2\bcrypt-0.10.2.jar;C:\Users\Fluorum\.m2\repository\at\favre\lib\bytes\1.5.0\bytes-1.5.0.jar;C:\Users\Fluorum\.m2\repository\org\jetbrains\annotations\24.0.0\annotations-24.0.0.jar;C:\Users\Fluorum\.m2\repository\org\postgresql\postgresql\42.6.0\postgresql-42.6.0.jar;C:\Users\Fluorum\.m2\repository\org\checkerframework\checker-qual\3.31.0\checker-qual-3.31.0.jar Starter
Exception in thread "main" java.lang.ClassFormatError: Illegal field name "" in class Helper
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1027)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:862)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:760)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:681)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:639)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
at Starter.main(Starter.java:7)
Process finished with exit code 1
代码从哪里获取空字段?唯一的猜测是哈希映射键。但在这一行中它们不是空的?
UPD我试图进行标注:
public class Starter {
@Contract("_ -> new")
public static @NotNull Helper.ConnectionPath parseURI (@NotNull String uri) {
int index = uri.indexOf('?');
String path = uri.substring(0, index);
String query = uri.substring(index + 1);
String[] pathParts = path.split("/");
System.out.println(Arrays.toString(pathParts));
Map<String, String> params = new HashMap<>();
for (String param : query.split("&")) {
String[] entry = param.split("=");
System.out.println("-----");
System.out.println(Arrays.toString(entry));
params.put(entry[0], entry[1]);
}
return new Helper.ConnectionPath(pathParts, params);
}
public static void main (String[] args) {
Helper.ConnectionPath returner = parseURI("/id=4&token=user.4.wsm3mOlDwbhaHD0iG33nQ9CwBEIf3B7al5Bylki1XpuKOHAxy2og1mmqdIs5yALmW");
System.out.println("Прошло!");
System.out.println(returner);
}
}
但现在错误完全不同了:
C:\Users\Fluorum\.jdks\sapmachine-21\bin\java.exe --enable-preview "-javaagent:C:\Users\Fluorum\AppData\Local\Programs\IntelliJ IDEA Community Edition\lib\idea_rt.jar=60335:C:\Users\Fluorum\AppData\Local\Programs\IntelliJ IDEA Community Edition\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath E:\Projects\GigaChat\RTCD\out\production\RTCD;C:\Users\Fluorum\.m2\repository\com\jsoniter\jsoniter\0.9.23\jsoniter-0.9.23.jar;C:\Users\Fluorum\.m2\repository\org\java-websocket\Java-WebSocket\1.5.4\Java-WebSocket-1.5.4.jar;C:\Users\Fluorum\.m2\repository\org\slf4j\slf4j-api\2.0.6\slf4j-api-2.0.6.jar;C:\Users\Fluorum\.m2\repository\at\favre\lib\bcrypt\0.10.2\bcrypt-0.10.2.jar;C:\Users\Fluorum\.m2\repository\at\favre\lib\bytes\1.5.0\bytes-1.5.0.jar;C:\Users\Fluorum\.m2\repository\org\jetbrains\annotations\24.0.0\annotations-24.0.0.jar;C:\Users\Fluorum\.m2\repository\org\postgresql\postgresql\42.6.0\postgresql-42.6.0.jar;C:\Users\Fluorum\.m2\repository\org\checkerframework\checker-qual\3.31.0\checker-qual-3.31.0.jar Starter
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: Range [0, -1) out of bounds for length 84
at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)
at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromToIndex(Preconditions.java:112)
at java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:349)
at java.base/java.lang.String.checkBoundsBeginEnd(String.java:4861)
at java.base/java.lang.String.substring(String.java:2830)
at Starter.parseURI(Starter.java:12)
at Starter.main(Starter.java:35)
Process finished with exit code 1
UDP 2我又做了一些实验。我发现,如果我注释掉该方法SHA512
(通过 的哈希函数SHA-512
),错误就会消失并且一切正常。
经过实验,我发现了问题所在。结果发现它与解析器完全无关。出于某种原因,尽管21个Java,他并没有错过这段代码(虽然IDEA不骂人):
虽然这很平静: