Added Parity/Sanity to Scrambling

This commit is contained in:
Sn0wStorm 2016-06-29 23:24:39 +02:00
parent e6b68a2d7a
commit d3ec4181f2
4 changed files with 15 additions and 5 deletions

View File

@ -16,6 +16,7 @@ import org.bukkit.potion.PotionType;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -752,6 +753,9 @@ public class Brew {
} catch (IOException e) { } catch (IOException e) {
P.p.errorLog("IO Error while loading Brew"); P.p.errorLog("IO Error while loading Brew");
e.printStackTrace(); e.printStackTrace();
} catch (InvalidKeyException e) {
P.p.errorLog("Failed to load Brew, has the data key 'BrewDataSeed' in the data.yml been changed?");
e.printStackTrace();
} }
return null; return null;
} }
@ -836,12 +840,12 @@ public class Brew {
} }
public static void writeSeed(ConfigurationSection section) { public static void writeSeed(ConfigurationSection section) {
section.set("seed", saveSeed); section.set("BrewDataSeed", saveSeed);
} }
public static void loadSeed(ConfigurationSection section) { public static void loadSeed(ConfigurationSection section) {
if (section.contains("seed")) { if (section.contains("BrewDataSeed")) {
saveSeed = section.getLong("seed"); saveSeed = section.getLong("BrewDataSeed");
} else { } else {
while (saveSeed == 0) { while (saveSeed == 0) {
saveSeed = new SecureRandom().nextLong(); saveSeed = new SecureRandom().nextLong();

View File

@ -179,6 +179,8 @@ public class P extends JavaPlugin {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} finally { } finally {
try { try {
data.close(); data.close();

View File

@ -1,6 +1,5 @@
package com.dre.brewery.lore; package com.dre.brewery.lore;
import java.io.FilterOutputStream; import java.io.FilterOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
@ -27,6 +26,7 @@ public class XORScrambleStream extends FilterOutputStream {
xorStream = new SeedInputStream(seed ^ id); xorStream = new SeedInputStream(seed ^ id);
out.write((byte) (id >> 8)); out.write((byte) (id >> 8));
out.write((byte) id); out.write((byte) id);
write(209); // parity/sanity
} }
} }

View File

@ -3,6 +3,7 @@ package com.dre.brewery.lore;
import java.io.FilterInputStream; import java.io.FilterInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.security.InvalidKeyException;
public class XORUnscrambleStream extends FilterInputStream { public class XORUnscrambleStream extends FilterInputStream {
@ -17,7 +18,7 @@ public class XORUnscrambleStream extends FilterInputStream {
this.seed = seed; this.seed = seed;
} }
public void start() throws IOException { public void start() throws IOException, InvalidKeyException {
running = true; running = true;
if (xorStream == null) { if (xorStream == null) {
short id = (short) (in.read() << 8 | in.read()); short id = (short) (in.read() << 8 | in.read());
@ -26,6 +27,9 @@ public class XORUnscrambleStream extends FilterInputStream {
return; return;
} }
xorStream = new SeedInputStream(seed ^ id); xorStream = new SeedInputStream(seed ^ id);
if (read() != 209) { // Parity/Sanity
throw new InvalidKeyException("Could not read scrambled data, is the seed wrong?");
}
} }
} }