updated Random() to SecureRandom() (#1670)
* updated Random() to SecureRandom() * batch 2 of SecureRandom updates
This commit is contained in:
parent
c150871a94
commit
b423002e6c
@ -23,8 +23,8 @@
|
||||
|
||||
package com.iluwatar.commander;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Order class holds details of the order.
|
||||
@ -45,7 +45,7 @@ public class Order { //can store all transactions ids also
|
||||
public final String id;
|
||||
final float price;
|
||||
final long createdTime;
|
||||
private static final Random RANDOM = new Random();
|
||||
private static final SecureRandom RANDOM = new SecureRandom();
|
||||
private static final String ALL_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
|
||||
private static final Hashtable<String, Boolean> USED_IDS = new Hashtable<String, Boolean>();
|
||||
PaymentStatus paid;
|
||||
|
@ -23,10 +23,10 @@
|
||||
|
||||
package com.iluwatar.commander;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
@ -56,7 +56,7 @@ public class Retry<T> {
|
||||
void handleIssue(T obj, Exception e);
|
||||
}
|
||||
|
||||
private static final Random RANDOM = new Random();
|
||||
private static final SecureRandom RANDOM = new SecureRandom();
|
||||
|
||||
private final Operation op;
|
||||
private final HandleErrorIssue<T> handleError;
|
||||
|
@ -24,10 +24,11 @@
|
||||
package com.iluwatar.commander;
|
||||
|
||||
import com.iluwatar.commander.exceptions.DatabaseUnavailableException;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Hashtable;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Service class is an abstract class extended by all services in this example. They all have a
|
||||
@ -42,7 +43,7 @@ public abstract class Service {
|
||||
|
||||
protected final Database database;
|
||||
public ArrayList<Exception> exceptionsList;
|
||||
private static final Random RANDOM = new Random();
|
||||
private static final SecureRandom RANDOM = new SecureRandom();
|
||||
private static final String ALL_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
|
||||
private static final Hashtable<String, Boolean> USED_IDS = new Hashtable<>();
|
||||
|
||||
|
@ -23,8 +23,8 @@
|
||||
|
||||
package com.iluwatar.event.asynchronous;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
@ -43,7 +43,7 @@ public class EventManager implements ThreadCompleteListener {
|
||||
public static final int MAX_ID = MAX_RUNNING_EVENTS;
|
||||
public static final int MAX_EVENT_TIME = 1800; // in seconds / 30 minutes.
|
||||
private int currentlyRunningSyncEvent = -1;
|
||||
private final Random rand;
|
||||
private final SecureRandom rand;
|
||||
private final Map<Integer, Event> eventPool;
|
||||
|
||||
private static final String DOES_NOT_EXIST = " does not exist.";
|
||||
@ -52,7 +52,7 @@ public class EventManager implements ThreadCompleteListener {
|
||||
* EventManager constructor.
|
||||
*/
|
||||
public EventManager() {
|
||||
rand = new Random(1);
|
||||
rand = new SecureRandom();
|
||||
eventPool = new ConcurrentHashMap<Integer, Event>(MAX_RUNNING_EVENTS);
|
||||
|
||||
}
|
||||
|
@ -23,7 +23,7 @@
|
||||
|
||||
package com.iluwatar.gameloop;
|
||||
|
||||
import java.util.Random;
|
||||
import java.security.SecureRandom;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -80,7 +80,7 @@ public abstract class GameLoop {
|
||||
*/
|
||||
protected void processInput() {
|
||||
try {
|
||||
var lag = new Random().nextInt(200) + 50;
|
||||
var lag = new SecureRandom().nextInt(200) + 50;
|
||||
Thread.sleep(lag);
|
||||
} catch (InterruptedException e) {
|
||||
logger.error(e.getMessage());
|
||||
|
@ -24,10 +24,10 @@
|
||||
package com.iluwatar.hexagonal.domain;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.PrimitiveIterator;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@ -126,7 +126,7 @@ public class LotteryNumbers {
|
||||
* @param max the max value (inclusive)
|
||||
*/
|
||||
public RandomNumberGenerator(int min, int max) {
|
||||
randomIterator = new Random().ints(min, max + 1).iterator();
|
||||
randomIterator = new SecureRandom().ints(min, max + 1).iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -30,9 +30,9 @@ import com.iluwatar.hexagonal.domain.LotteryService;
|
||||
import com.iluwatar.hexagonal.domain.LotteryTicket;
|
||||
import com.iluwatar.hexagonal.domain.LotteryTicketId;
|
||||
import com.iluwatar.hexagonal.domain.PlayerDetails;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.AbstractMap.SimpleEntry;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@ -41,7 +41,7 @@ import java.util.stream.Collectors;
|
||||
public class SampleData {
|
||||
|
||||
private static final List<PlayerDetails> PLAYERS;
|
||||
private static final Random RANDOM = new Random();
|
||||
private static final SecureRandom RANDOM = new SecureRandom();
|
||||
|
||||
static {
|
||||
PLAYERS = List.of(
|
||||
|
@ -23,7 +23,7 @@
|
||||
|
||||
package com.iluwatar.leaderfollowers;
|
||||
|
||||
import java.util.Random;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@ -84,7 +84,7 @@ public class App {
|
||||
* Add tasks.
|
||||
*/
|
||||
private static void addTasks(TaskSet taskSet) throws InterruptedException {
|
||||
var rand = new Random();
|
||||
var rand = new SecureRandom();
|
||||
for (var i = 0; i < 5; i++) {
|
||||
var time = Math.abs(rand.nextInt(1000));
|
||||
taskSet.addTask(new Task(time));
|
||||
|
@ -23,7 +23,7 @@
|
||||
|
||||
package com.iluwatar.masterworker;
|
||||
|
||||
import java.util.Random;
|
||||
import java.security.SecureRandom;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -35,7 +35,7 @@ public class ArrayUtilityMethods {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ArrayUtilityMethods.class);
|
||||
|
||||
private static final Random RANDOM = new Random();
|
||||
private static final SecureRandom RANDOM = new SecureRandom();
|
||||
|
||||
/**
|
||||
* Method arraysSame compares 2 arrays @param a1 and @param a2 and @return whether their values
|
||||
|
@ -23,7 +23,7 @@
|
||||
|
||||
package com.iluwatar.producer.consumer;
|
||||
|
||||
import java.util.Random;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
/**
|
||||
* Class responsible for producing unit of work that can be expressed as {@link Item} and submitted
|
||||
@ -31,7 +31,7 @@ import java.util.Random;
|
||||
*/
|
||||
public class Producer {
|
||||
|
||||
private static final Random RANDOM = new Random();
|
||||
private static final SecureRandom RANDOM = new SecureRandom();
|
||||
|
||||
private final ItemQueue queue;
|
||||
|
||||
|
@ -23,9 +23,9 @@
|
||||
|
||||
package com.iluwatar.sharding;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -58,7 +58,7 @@ public class LookupShardManager extends ShardManager {
|
||||
return lookupMap.get(key);
|
||||
} else {
|
||||
var shardCount = shardMap.size();
|
||||
return new Random().nextInt(shardCount - 1) + 1;
|
||||
return new SecureRandom().nextInt(shardCount - 1) + 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -23,8 +23,8 @@
|
||||
|
||||
package com.iluwatar.spatialpartition;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.HashMap;
|
||||
import java.util.Random;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -112,7 +112,7 @@ public class App {
|
||||
public static void main(String[] args) {
|
||||
var bubbles1 = new HashMap<Integer, Bubble>();
|
||||
var bubbles2 = new HashMap<Integer, Bubble>();
|
||||
var rand = new Random();
|
||||
var rand = new SecureRandom();
|
||||
for (int i = 0; i < 10000; i++) {
|
||||
var b = new Bubble(rand.nextInt(300), rand.nextInt(300), i, rand.nextInt(2) + 1);
|
||||
bubbles1.put(i, b);
|
||||
|
@ -23,9 +23,9 @@
|
||||
|
||||
package com.iluwatar.spatialpartition;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Random;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -36,7 +36,7 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
public class Bubble extends Point<Bubble> {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Bubble.class);
|
||||
private static final Random RANDOM = new Random();
|
||||
private static final SecureRandom RANDOM = new SecureRandom();
|
||||
|
||||
final int radius;
|
||||
|
||||
|
@ -26,9 +26,9 @@ package com.iluwatar.typeobject;
|
||||
import com.iluwatar.typeobject.Candy.Type;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
/**
|
||||
@ -38,7 +38,7 @@ import org.json.simple.parser.ParseException;
|
||||
*/
|
||||
|
||||
public class CellPool {
|
||||
private static final Random RANDOM = new Random();
|
||||
private static final SecureRandom RANDOM = new SecureRandom();
|
||||
public static final String FRUIT = "fruit";
|
||||
public static final String CANDY = "candy";
|
||||
List<Cell> pool;
|
||||
|
@ -23,9 +23,9 @@
|
||||
|
||||
package com.iluwatar.updatemethod;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -66,7 +66,7 @@ public class World {
|
||||
*/
|
||||
private void processInput() {
|
||||
try {
|
||||
int lag = new Random().nextInt(200) + 50;
|
||||
int lag = new SecureRandom().nextInt(200) + 50;
|
||||
Thread.sleep(lag);
|
||||
} catch (InterruptedException e) {
|
||||
LOGGER.error(e.getMessage());
|
||||
|
Loading…
x
Reference in New Issue
Block a user