Зделал всё только на база64 без всяких урл сафе

Вроде всё работает
тест весь проходит
This commit is contained in:
AidarKC
2026-01-29 19:29:08 +03:00
parent 0b2bee0a3d
commit b5706d3ed5
13 changed files with 102 additions and 86 deletions
@@ -0,0 +1,40 @@
package server.logic.ws_protocol;
import java.util.Base64;
/**
* Единая утилита Base64 для всего WS-протокола.
*
* ВАЖНО:
* - Используем ТОЛЬКО стандартный Base64 (RFC 4648) алфавит: '+' и '/'.
* - Без padding '=' (чтобы строки были короче и стабильнее для JSON).
* - Декодер при этом спокойно принимает и с '=' и без '='.
*/
public final class Base64Ws {
private static final Base64.Encoder ENC = Base64.getEncoder().withoutPadding();
private static final Base64.Decoder DEC = Base64.getDecoder();
private Base64Ws() {}
public static String encode(byte[] bytes) {
if (bytes == null) throw new IllegalArgumentException("bytes == null");
return ENC.encodeToString(bytes);
}
public static byte[] decode(String b64) throws IllegalArgumentException {
if (b64 == null) throw new IllegalArgumentException("base64 is null");
String s = b64.trim();
if (s.isEmpty()) throw new IllegalArgumentException("base64 is empty");
return DEC.decode(s);
}
public static byte[] decodeLen(String b64, int expectedLen, String fieldName) throws IllegalArgumentException {
byte[] v = decode(b64);
if (v.length != expectedLen) {
String f = (fieldName == null || fieldName.isBlank()) ? "value" : fieldName;
throw new IllegalArgumentException(f + " must be " + expectedLen + " bytes, got " + v.length);
}
return v;
}
}