Skip to content

Commit 6c79fe3

Browse files
committed
Add new unsecureRandom() constructor to create fast and/or deterministic pseudo random values
1 parent 0460836 commit 6c79fe3

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## v1.1.0
4+
5+
* add `unsecureRandom()` constructor which creates random for e.g. tests or deterministic randoms
6+
37
## v1.0.0
48

59
* add `append()` method supporting multiple byte arrays #26

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,18 @@ Bytes.allocate(4, (byte) 1); //fill with 0x01
145145
Bytes.empty(); //creates zero length byte array
146146
```
147147

148-
Creating **random** byte arrays for e.g. testing:
148+
Creating cryptographically secure **random** byte arrays:
149149

150150
```java
151151
Bytes.random(12);
152152
```
153153

154+
Creating cryptographically unsecure **random** byte arrays for e.g. testing:
155+
156+
```java
157+
Bytes.unsecureRandom(12, 12345L); // using seed makes it deterministic
158+
```
159+
154160
Reading byte content of encoded `String`s:
155161

156162
```java

src/main/java/at/favre/lib/bytes/Bytes.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,35 @@ public static Bytes random(int length) {
662662
return random(length, new SecureRandom());
663663
}
664664

665+
/**
666+
* A new instance with pseudo random bytes using an unsecure random number generator.
667+
* This may be used in e.g. tests. In production code use {@link #random(int)} per default.
668+
* <p>
669+
* <strong>ONLY USE IN NON-SECURITY RELEVANT CONTEXT!</strong>
670+
*
671+
* @param length desired array length
672+
* @return random instance
673+
*/
674+
public static Bytes unsecureRandom(int length) {
675+
return random(length, new Random());
676+
}
677+
678+
/**
679+
* A new instance with pseudo random bytes using an unsecure random number generator.
680+
* This may be used in e.g. tests to create predictable numbers.
681+
* <p>
682+
* In production code use {@link #random(int)} per default.
683+
* <p>
684+
* <strong>ONLY USE IN NON-SECURITY RELEVANT CONTEXT!</strong>
685+
*
686+
* @param length desired array length
687+
* @param seed used to seed random number generator - using same seed will generate same numbers
688+
* @return random instance
689+
*/
690+
public static Bytes unsecureRandom(int length, long seed) {
691+
return random(length, new Random(seed));
692+
}
693+
665694
/**
666695
* A new instance with random bytes.
667696
*

src/test/java/at/favre/lib/bytes/BytesConstructorTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.nio.IntBuffer;
3535
import java.nio.charset.Charset;
3636
import java.nio.charset.StandardCharsets;
37+
import java.security.SecureRandom;
3738
import java.text.Normalizer;
3839
import java.util.Arrays;
3940
import java.util.BitSet;
@@ -547,4 +548,25 @@ private void testUUID(UUID uuid) {
547548
public void fromUUIDNullArgument() {
548549
Bytes.from((UUID) null);
549550
}
551+
552+
@Test
553+
public void createSecureRandom() {
554+
assertNotEquals(Bytes.random(16), Bytes.random(16));
555+
}
556+
557+
@Test
558+
public void createSecureRandomWithExplicitSecureRandom() {
559+
assertNotEquals(Bytes.random(16, new SecureRandom()), Bytes.random(16, new SecureRandom()));
560+
}
561+
562+
@Test
563+
public void createUnsecureRandom() {
564+
assertNotEquals(Bytes.unsecureRandom(128), Bytes.unsecureRandom(128));
565+
}
566+
567+
@Test
568+
public void createUnsecureRandomWithSeed() {
569+
assertEquals(Bytes.unsecureRandom(128, 4L), Bytes.unsecureRandom(128, 4L));
570+
assertNotEquals(Bytes.unsecureRandom(4, 3L), Bytes.unsecureRandom(4, 4L));
571+
}
550572
}

0 commit comments

Comments
 (0)