Skip to content

Commit

Permalink
Don't use a blocking algorithm for generating keys on unix-like systems
Browse files Browse the repository at this point in the history
This should fix GeyserMC/Floodgate#125
  • Loading branch information
Tim203 committed Mar 31, 2021
1 parent 677a8d6 commit 5c12dc8
Showing 1 changed file with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Locale;

public final class AesKeyProducer implements KeyProducer {
public static int KEY_SIZE = 128;
Expand All @@ -38,7 +40,7 @@ public final class AesKeyProducer implements KeyProducer {
public SecretKey produce() {
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(KEY_SIZE, SecureRandom.getInstanceStrong());
keyGenerator.init(KEY_SIZE, getSecureRandom());
return keyGenerator.generateKey();
} catch (Exception exception) {
throw new RuntimeException(exception);
Expand All @@ -53,4 +55,14 @@ public SecretKey produceFrom(byte[] keyFileData) {
throw new RuntimeException(exception);
}
}

private SecureRandom getSecureRandom() throws NoSuchAlgorithmException {
// use Windows-PRNG for windows (default impl is SHA1PRNG)
// default impl for unix-like systems is NativePRNG.
if (System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("win")) {
return SecureRandom.getInstance("Windows-PRNG");
} else {
return new SecureRandom();
}
}
}

0 comments on commit 5c12dc8

Please sign in to comment.