RandomGenerator.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.utils.random;

  21. import java.util.HashMap;
  22. import java.util.Map;

  23. import org.openspcoop2.utils.UtilsRuntimeException;
  24. import org.openspcoop2.utils.io.Base64Utilities;

  25. /**
  26.  * RandomGenerator
  27.  *
  28.  * @author Andrea Poli (poli@link.it)
  29.  * @author $Author$
  30.  * @version $Rev$, $Date$
  31.  */

  32. public class RandomGenerator {

  33.     private boolean useSecureRandom = false;
  34.     // https://docs.oracle.com/en/java/javase/11/docs/specs/security/standard-names.html#securerandom-number-generation-algorithms
  35.     private String algorithmSecureRandom = null;
  36.    
  37.     public boolean isUseSecureRandom() {
  38.         return this.useSecureRandom;
  39.     }
  40.     public void setUseSecureRandom(boolean useSecureRandom) {
  41.         this.useSecureRandom = useSecureRandom;
  42.     }
  43.     public String getAlgorithmSecureRandom() {
  44.         return this.algorithmSecureRandom;
  45.     }
  46.     public void setAlgorithmSecureRandom(String algorithmSecureRandom) {
  47.         this.algorithmSecureRandom = algorithmSecureRandom;
  48.     }
  49.    
  50.     public static RandomGenerator getDefaultInstance() {
  51.         return new RandomGenerator(true);
  52.     }
  53.    
  54.     public RandomGenerator() {
  55.         initEngine(false, null);
  56.     }
  57.     public RandomGenerator(boolean useSecureRandom) {
  58.         initEngine(useSecureRandom, null);
  59.     }
  60.     public RandomGenerator(boolean useSecureRandom, SecureRandomAlgorithm algo) {
  61.         initEngine(useSecureRandom, algo.getValue());
  62.     }
  63.     public RandomGenerator(boolean useSecureRandom, String algorithm) {
  64.         initEngine(useSecureRandom, algorithm);
  65.     }
  66.     private void initEngine(boolean useSecureRandom, String algorithm) {
  67.         this.useSecureRandom = useSecureRandom;
  68.         this.algorithmSecureRandom = algorithm;
  69.     }
  70.    
  71.     public String nextRandom(int length) {
  72.         StringBuilder sb = new StringBuilder();
  73.         for (int i = 0; i < length; i++) {
  74.             sb.append(Base64Utilities.B64_STRING.charAt(this.nextInt(Base64Utilities.B64_STRING.length())));
  75.         }
  76.         return sb.toString();
  77.     }
  78.    
  79.     public String nextRandomInt(int length) {
  80.         StringBuilder sb = new StringBuilder();
  81.         for (int i = 0; i < length; i++) {
  82.             sb.append(this.nextInt(10));
  83.         }
  84.         return sb.toString();
  85.     }

  86.     public byte[] nextRandomBytes(int length) {
  87.         byte[] arrByte = new byte[length];
  88.         nextRandomBytes(arrByte);
  89.         return arrByte;
  90.     }
  91.     public void nextRandomBytes(byte[] arrByte) {
  92.         if(this.useSecureRandom) {
  93.             java.security.SecureRandom sr = (java.security.SecureRandom) getRandomEngine();
  94.             sr.nextBytes(arrByte);
  95.         }
  96.         else {
  97.             java.util.Random rd = (java.util.Random) getRandomEngine();
  98.             rd.nextBytes(arrByte);
  99.         }
  100.     }
  101.    
  102.     public boolean nextBoolean() {
  103.         if(this.useSecureRandom) {
  104.             java.security.SecureRandom sr = (java.security.SecureRandom) getRandomEngine();
  105.             return sr.nextBoolean();
  106.         }
  107.         else {
  108.             java.util.Random rd = (java.util.Random) getRandomEngine();
  109.             return rd.nextBoolean();
  110.         }
  111.     }
  112.    
  113.     public int nextInt() {
  114.         if(this.useSecureRandom) {
  115.             java.security.SecureRandom sr = (java.security.SecureRandom) getRandomEngine();
  116.             return sr.nextInt();
  117.         }
  118.         else {
  119.             java.util.Random rd = (java.util.Random) getRandomEngine();
  120.             return rd.nextInt();
  121.         }
  122.     }
  123.     public int nextInt(int bound) {
  124.         if(this.useSecureRandom) {
  125.             java.security.SecureRandom sr = (java.security.SecureRandom) getRandomEngine();
  126.             return sr.nextInt(bound);
  127.         }
  128.         else {
  129.             java.util.Random rd = (java.util.Random) getRandomEngine();
  130.             return rd.nextInt(bound);
  131.         }
  132.     }
  133.     public long nextLong() {
  134.         if(this.useSecureRandom) {
  135.             java.security.SecureRandom sr = (java.security.SecureRandom) getRandomEngine();
  136.             return sr.nextLong();
  137.         }
  138.         else {
  139.             java.util.Random rd = (java.util.Random) getRandomEngine();
  140.             return rd.nextLong();
  141.         }
  142.     }
  143.    
  144.     public double nextDouble() {
  145.         if(this.useSecureRandom) {
  146.             java.security.SecureRandom sr = (java.security.SecureRandom) getRandomEngine();
  147.             return sr.nextDouble();
  148.         }
  149.         else {
  150.             java.util.Random rd = (java.util.Random) getRandomEngine();
  151.             return rd.nextDouble();
  152.         }
  153.     }
  154.     public float nextFloat() {
  155.         if(this.useSecureRandom) {
  156.             java.security.SecureRandom sr = (java.security.SecureRandom) getRandomEngine();
  157.             return sr.nextFloat();
  158.         }
  159.         else {
  160.             java.util.Random rd = (java.util.Random) getRandomEngine();
  161.             return rd.nextFloat();
  162.         }
  163.     }
  164.     public double nextGaussian() {
  165.         if(this.useSecureRandom) {
  166.             java.security.SecureRandom sr = (java.security.SecureRandom) getRandomEngine();
  167.             return sr.nextGaussian();
  168.         }
  169.         else {
  170.             java.util.Random rd = (java.util.Random) getRandomEngine();
  171.             return rd.nextGaussian();
  172.         }
  173.     }
  174.    
  175.     public String getAlgorithm() {
  176.         if(this.useSecureRandom) {
  177.             java.security.SecureRandom sr = (java.security.SecureRandom) getRandomEngine();
  178.             return sr.getAlgorithm();
  179.         }
  180.         else {
  181.             java.util.Random rd = (java.util.Random) getRandomEngine();
  182.             return rd.getClass().getName();
  183.         }
  184.     }
  185.    
  186.    
  187.     public Object getRandomEngine() {
  188.         if(this.useSecureRandom) {
  189.             java.security.SecureRandom sr = null;
  190.             if(this.algorithmSecureRandom!=null) {
  191.                 sr = getSecureRandom(this.algorithmSecureRandom);
  192.             }
  193.             else {
  194.                 sr = getSecureRandomDefault();
  195.             }
  196.             /**System.out.println("algorithmSecureRandom: "+sr.getAlgorithm());*/
  197.             return sr;
  198.         }
  199.         else {
  200.             return getSecureRandomDefault();
  201.         }
  202.     }
  203.    
  204.    
  205.     private static Map<String, java.security.SecureRandom> mapAlgoSecureRandom = new HashMap<>();
  206.     private static synchronized void initSecureRandom(String algorithmSecureRandom){
  207.         if(!mapAlgoSecureRandom.containsKey(algorithmSecureRandom)) {
  208.             try {
  209.                 java.security.SecureRandom sr = java.security.SecureRandom.getInstance(algorithmSecureRandom);
  210.                 mapAlgoSecureRandom.put(algorithmSecureRandom, sr);
  211.             }catch(Exception e) {
  212.                 throw new UtilsRuntimeException(e.getMessage(),e);
  213.             }
  214.         }
  215.     }
  216.     private static java.security.SecureRandom getSecureRandom(String algorithmSecureRandom){
  217.         if(!mapAlgoSecureRandom.containsKey(algorithmSecureRandom)) {
  218.             initSecureRandom(algorithmSecureRandom);
  219.         }
  220.         return mapAlgoSecureRandom.get(algorithmSecureRandom);
  221.     }
  222.    
  223.     private static java.security.SecureRandom secureRandomDefault = null;
  224.     private static synchronized void initSecureRandomDefault(){
  225.         if(secureRandomDefault==null) {
  226.             secureRandomDefault = new java.security.SecureRandom();
  227.         }
  228.     }
  229.     private static java.security.SecureRandom getSecureRandomDefault(){
  230.         if(secureRandomDefault==null) {
  231.             initSecureRandomDefault();
  232.         }
  233.         return secureRandomDefault;
  234.     }
  235. }