Added XOR Scrambler for Brew data

Very fast obfuscator for brew data so that a client cant easily read it
This commit is contained in:
Sn0wStorm
2019-10-11 18:05:49 +02:00
parent cdb36b1736
commit 9bf3268fb4
6 changed files with 430 additions and 11 deletions
+33 -11
View File
@@ -1,10 +1,7 @@
package com.dre.brewery;
import org.bukkit.Color;
import com.dre.brewery.lore.Base91DecoderStream;
import com.dre.brewery.lore.Base91EncoderStream;
import com.dre.brewery.lore.LoreLoadStream;
import com.dre.brewery.lore.LoreSaveStream;
import com.dre.brewery.lore.*;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.BrewerInventory;
@@ -20,6 +17,7 @@ import org.bukkit.potion.PotionType;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -28,7 +26,7 @@ import java.util.Map;
public class Brew {
// represents the liquid in the brewed Potions
private static long saveSeed;
public static Map<Integer, Brew> legacyPotions = new HashMap<>();
public static long installTime = System.currentTimeMillis(); // plugin install time in millis after epoch
public static Boolean colorInBarrels; // color the Lore while in Barrels
@@ -732,18 +730,26 @@ public class Brew {
} catch (IllegalArgumentException ignored) {
return null;
}
try (DataInputStream in = new DataInputStream(new Base91DecoderStream(loreStream))) {
XORUnscrambleStream unscrambler = new XORUnscrambleStream(new Base91DecoderStream(loreStream), saveSeed);
try (DataInputStream in = new DataInputStream(unscrambler)) {
boolean parityFailed = false;
if (in.readByte() != 86) {
P.p.errorLog("parity check failed on Brew while loading, trying to load anyways!");
P.p.errorLog("Parity check failed on Brew while loading, trying to load anyways!");
parityFailed = true;
}
Brew brew = new Brew();
byte ver = in.readByte();
switch (ver) {
case 1:
unscrambler.start();
brew.loadFromStream(in);
break;
default:
P.p.errorLog("Brew has data stored in v" + ver + " this version supports up to v1");
if (parityFailed) {
P.p.errorLog("Failed to load Brew. Maybe something corrupted the Lore of the Item?");
} else {
P.p.errorLog("Brew has data stored in v" + ver + " this Plugin version supports up to v1");
}
return null;
}
return brew;
@@ -779,7 +785,11 @@ public class Brew {
// Save brew data into meta/lore
public void save(ItemMeta meta) {
try (DataOutputStream out = new DataOutputStream(new Base91EncoderStream(new LoreSaveStream(meta, 0)))) {
XORScrambleStream scrambler = new XORScrambleStream(new Base91EncoderStream(new LoreSaveStream(meta, 0)), saveSeed);
try (DataOutputStream out = new DataOutputStream(scrambler)) {
out.writeByte(86); // Parity/sanity
out.writeByte(1); // Version
scrambler.start();
saveToStream(out);
} catch (IOException e) {
P.p.errorLog("IO Error while saving Brew");
@@ -801,8 +811,6 @@ public class Brew {
}
public void saveToStream(DataOutputStream out) throws IOException {
out.writeByte(86); // Parity/sanity
out.writeByte(1); // Version
if (quality > 10) {
quality = 10;
}
@@ -831,6 +839,20 @@ public class Brew {
ingredients.save(out);
}
public static void writeSeed(ConfigurationSection section) {
section.set("seed", saveSeed);
}
public static void loadSeed(ConfigurationSection section) {
if (section.contains("seed")) {
saveSeed = section.getLong("seed");
} else {
while (saveSeed == 0) {
saveSeed = new SecureRandom().nextLong();
}
}
}
// Load potion data from data file for backwards compatibility
public static void loadLegacy(BIngredients ingredients, int uid, int quality, byte distillRuns, float ageTime, float wood, String recipe, boolean unlabeled, boolean persistent, boolean stat) {
Brew brew = new Brew(ingredients, quality, distillRuns, ageTime, wood, recipe, unlabeled, stat);