|
| 1 | +/* |
| 2 | + * java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project. |
| 3 | + * Copyright (C) 2018-2025 Tilman Neumann - tilman.neumann@web.de |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License |
| 6 | + * as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
| 9 | + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 10 | + * |
| 11 | + * You should have received a copy of the GNU General Public License along with this program; |
| 12 | + * if not, see <http://www.gnu.org/licenses/>. |
| 13 | + */ |
| 14 | +package de.tilman_neumann.jml.random; |
| 15 | + |
| 16 | +import java.security.SecureRandom; |
| 17 | +import java.util.ArrayList; |
| 18 | + |
| 19 | +import org.junit.BeforeClass; |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +import de.tilman_neumann.util.ConfigUtil; |
| 23 | + |
| 24 | +import static org.junit.Assert.assertNotEquals; |
| 25 | + |
| 26 | +public class SecureRandomTest { |
| 27 | + |
| 28 | + @BeforeClass |
| 29 | + public static void setup() { |
| 30 | + ConfigUtil.initProject(); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void secureRandomSequenceIsNotReproducible() { |
| 35 | + SecureRandom secureRandom = new SecureRandom(); |
| 36 | + long seed = 2487568097L; |
| 37 | + |
| 38 | + secureRandom.setSeed(seed); |
| 39 | + ArrayList<Integer> secureList1 = new ArrayList<>(); |
| 40 | + for (int n=0; n<20; n++) { |
| 41 | + secureList1.add(secureRandom.nextInt()); |
| 42 | + } |
| 43 | + |
| 44 | + secureRandom.setSeed(seed); |
| 45 | + ArrayList<Integer> secureList2 = new ArrayList<>(); |
| 46 | + for (int n=0; n<20; n++) { |
| 47 | + secureList2.add(secureRandom.nextInt()); |
| 48 | + } |
| 49 | + assertNotEquals(secureList1, secureList2); |
| 50 | + } |
| 51 | +} |
0 commit comments