Cryptographically secure signature

This commit is contained in:
Lixfel
2024-12-21 18:46:33 +01:00
parent 793fc31b5c
commit acbc5c5fd3
@@ -30,12 +30,24 @@ import de.steamwar.fightsystem.fight.FightTeam;
import de.steamwar.fightsystem.states.FightState;
import de.steamwar.fightsystem.states.StateDependent;
import de.steamwar.fightsystem.states.StateDependentListener;
import de.steamwar.sql.SteamwarUser;
import de.steamwar.techhider.TechHider;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.ConcurrentHashMap;
public class TechHiderWrapper extends StateDependent implements TechHider.LocationEvaluator, Listener {
@@ -43,12 +55,21 @@ public class TechHiderWrapper extends StateDependent implements TechHider.Locati
public static final boolean ENABLED = !Config.OnlyPublicSchematics && !Config.test() && Config.TechhiderActive;
private final ConcurrentHashMap<Player, Region> hiddenRegion = new ConcurrentHashMap<>();
private final ConcurrentHashMap<Player, Long> patterns = new ConcurrentHashMap<>();
private final TechHider techHider;
private final Cipher cipher;
public TechHiderWrapper() {
super(ENABLED, FightState.Schem);
techHider = new TechHider(this, Config.ObfuscateWith, Config.HiddenBlocks, Config.HiddenBlockEntities);
try {
cipher = Cipher.getInstance("AES_256/CFB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(Files.readAllBytes(new File(System.getProperty("user.home"), "hullhider.key").toPath()), "AES"));
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IOException e) {
throw new SecurityException(e);
}
new StateDependentListener(ENABLED, FightState.Schem, this);
register();
}
@@ -112,9 +133,8 @@ public class TechHiderWrapper extends StateDependent implements TechHider.Locati
public TechHider.State check(Player player, int x, int y, int z) {
if(hiddenRegion.computeIfAbsent(player, this::getHiddenRegion).inRegion(x, y, z)) {
if(FightSystem.getHullHider().isBlockHidden(player, x, y, z)) {
long pattern = player.getUniqueId().getLeastSignificantBits();
int id = ((y&3) << 4) + ((z&3) << 2) + (x&3);
return (pattern & (1L << id)) == 0 ? TechHider.State.HIDE : TechHider.State.HIDE_AIR;
return (patterns.computeIfAbsent(player, this::getPattern) & (1L << id)) == 0 ? TechHider.State.HIDE : TechHider.State.HIDE_AIR;
} else {
return TechHider.State.CHECK;
}
@@ -123,6 +143,14 @@ public class TechHiderWrapper extends StateDependent implements TechHider.Locati
}
}
public long getPattern(Player player) {
try {
return ByteBuffer.wrap(cipher.doFinal(ByteBuffer.wrap(new byte[8]).putLong(SteamwarUser.get(player.getUniqueId()).getId()).array())).getLong();
} catch (IllegalBlockSizeException | BadPaddingException e) {
throw new SecurityException(e);
}
}
@Override
public boolean blockPrecise(Player player, int chunkX, int chunkY, int chunkZ) {
return FightSystem.getHullHider().blockPrecise(player, chunkX, chunkY, chunkZ);