Now using SecureRandom.

This commit is contained in:
Erik C. Thauvin 2018-07-13 00:26:28 -07:00
parent ec753fdbd6
commit 7db6d76eea
3 changed files with 29 additions and 27 deletions

View file

@ -34,7 +34,7 @@ package net.thauvin.erik.mobibot.modules;
import net.thauvin.erik.mobibot.Mobibot; import net.thauvin.erik.mobibot.Mobibot;
import net.thauvin.erik.mobibot.Utils; import net.thauvin.erik.mobibot.Utils;
import java.util.Random; import java.security.SecureRandom;
/** /**
* The Dice module. * The Dice module.
@ -66,22 +66,22 @@ final public class Dice extends AbstractModule {
*/ */
@Override @Override
public void commandResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate) { public void commandResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate) {
final Random r = new Random(); final SecureRandom r = new SecureRandom();
int i = r.nextInt(6) + 1; int i = r.nextInt(6) + 1;
int y = r.nextInt(6) + 1; int y = r.nextInt(6) + 1;
final int playerTotal = i + y; final int playerTotal = i + y;
bot.send(bot.getChannel(), bot.send(bot.getChannel(),
sender + " rolled two dice: " + Utils.bold(i) + " and " + Utils.bold(y) + " for a total of " + Utils sender + " rolled two dice: " + Utils.bold(i) + " and " + Utils.bold(y) + " for a total of " + Utils
.bold(playerTotal)); .bold(playerTotal));
i = r.nextInt(6) + 1; i = r.nextInt(6) + 1;
y = r.nextInt(6) + 1; y = r.nextInt(6) + 1;
final int total = i + y; final int total = i + y;
bot.action( bot.action(
"rolled two dice: " + Utils.bold(i) + " and " + Utils.bold(y) + " for a total of " + Utils.bold(total)); "rolled two dice: " + Utils.bold(i) + " and " + Utils.bold(y) + " for a total of " + Utils.bold(total));
if (playerTotal < total) { if (playerTotal < total) {
bot.action("wins."); bot.action("wins.");

View file

@ -39,6 +39,7 @@ import java.io.BufferedReader;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
/** /**
* The Joke module. * The Joke module.
@ -55,7 +56,7 @@ final public class Joke extends AbstractModule {
// The ICNDB URL. // The ICNDB URL.
private static final String JOKE_URL = private static final String JOKE_URL =
"http://api.icndb.com/jokes/random?escape=javascript&exclude=[explicit]&limitTo=[nerdy]"; "http://api.icndb.com/jokes/random?escape=javascript&exclude=[explicit]&limitTo=[nerdy]";
/** /**
* Creates a new {@link Joke} instance. * Creates a new {@link Joke} instance.
@ -90,7 +91,8 @@ final public class Joke extends AbstractModule {
final URLConnection conn = url.openConnection(); final URLConnection conn = url.openConnection();
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
try (final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()))) { try (final BufferedReader reader =
new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
String line; String line;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
sb.append(line); sb.append(line);
@ -99,10 +101,10 @@ final public class Joke extends AbstractModule {
final JSONObject json = new JSONObject(sb.toString()); final JSONObject json = new JSONObject(sb.toString());
bot.send(bot.getChannel(), bot.send(bot.getChannel(),
Colors.CYAN Colors.CYAN
+ json.getJSONObject("value").get("joke").toString().replaceAll("\\'", "'") + json.getJSONObject("value").get("joke").toString().replaceAll("\\'", "'")
.replaceAll("\\\"", "\"") .replaceAll("\\\"", "\"")
+ Colors.NORMAL); + Colors.NORMAL);
} }
} catch (Exception e) { } catch (Exception e) {
bot.getLogger().warn("Unable to retrieve random joke.", e); bot.getLogger().warn("Unable to retrieve random joke.", e);

View file

@ -33,9 +33,9 @@ package net.thauvin.erik.mobibot.modules;
import net.thauvin.erik.mobibot.Mobibot; import net.thauvin.erik.mobibot.Mobibot;
import java.security.SecureRandom;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Random;
/** /**
* The Ping module. * The Ping module.
@ -52,19 +52,19 @@ public class Ping extends AbstractModule {
// The ping responses. // The ping responses.
private static final List<String> PINGS = private static final List<String> PINGS =
Arrays.asList( Arrays.asList(
"is barely alive.", "is barely alive.",
"is trying to stay awake.", "is trying to stay awake.",
"has gone fishing.", "has gone fishing.",
"is somewhere over the rainbow.", "is somewhere over the rainbow.",
"has fallen and can't get up.", "has fallen and can't get up.",
"is running. You better go chase it.", "is running. You better go chase it.",
"has just spontaneously combusted.", "has just spontaneously combusted.",
"is talking to itself... don't interrupt. That's rude.", "is talking to itself... don't interrupt. That's rude.",
"is bartending at an AA meeting.", "is bartending at an AA meeting.",
"is hibernating.", "is hibernating.",
"is saving energy: apathetic mode activated.", "is saving energy: apathetic mode activated.",
"is busy. Go away!"); "is busy. Go away!");
/** /**
* The default constructor. * The default constructor.
@ -78,7 +78,7 @@ public class Ping extends AbstractModule {
*/ */
@Override @Override
public void commandResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate) { public void commandResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate) {
final Random r = new Random(); final SecureRandom r = new SecureRandom();
bot.action(PINGS.get(r.nextInt(PINGS.size()))); bot.action(PINGS.get(r.nextInt(PINGS.size())));
} }
@ -91,4 +91,4 @@ public class Ping extends AbstractModule {
bot.send(sender, "To ping the bot:"); bot.send(sender, "To ping the bot:");
bot.send(sender, bot.helpIndent(bot.getNick() + ": " + PING_CMD)); bot.send(sender, bot.helpIndent(bot.getNick() + ": " + PING_CMD));
} }
} }