Added Parity/Sanity to Scrambling

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

View File

@ -17,6 +17,7 @@ import org.bukkit.potion.PotionType;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashMap;
@ -756,6 +757,9 @@ public class Brew {
} catch (IOException e) {
P.p.errorLog("IO Error while loading Brew");
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;
}
@ -840,12 +844,12 @@ public class Brew {
}
public static void writeSeed(ConfigurationSection section) {
section.set("seed", saveSeed);
section.set("BrewDataSeed", saveSeed);
}
public static void loadSeed(ConfigurationSection section) {
if (section.contains("seed")) {
saveSeed = section.getLong("seed");
if (section.contains("BrewDataSeed")) {
saveSeed = section.getLong("BrewDataSeed");
} else {
while (saveSeed == 0) {
saveSeed = new SecureRandom().nextLong();

View File

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

View File

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