Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion nostr-java-util/src/main/java/nostr/util/NostrUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
public class NostrUtil {

private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
private static final SecureRandom RANDOM = new SecureRandom();

public static String bytesToHex(byte[] b) {
char[] hexChars = new char[b.length * 2];
Expand Down Expand Up @@ -98,7 +99,7 @@ public static byte[] xor(byte[] b0, byte[] b1) {

public static byte[] createRandomByteArray(int len) {
byte[] b = new byte[len];
new SecureRandom().nextBytes(b);
RANDOM.nextBytes(b);
return b;
}

Expand Down
25 changes: 25 additions & 0 deletions nostr-java-util/src/test/java/nostr/util/NostrUtilRandomTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package nostr.util;

import org.junit.jupiter.api.Test;

import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.*;

public class NostrUtilRandomTest {

@Test
public void testCreateRandomByteArrayLength() {
int len = 16;
byte[] data = NostrUtil.createRandomByteArray(len);
assertNotNull(data, "Random byte array should not be null");
assertEquals(len, data.length, "Random byte array has wrong length");
}

@Test
public void testCreateRandomByteArrayUniqueness() {
byte[] data1 = NostrUtil.createRandomByteArray(16);
byte[] data2 = NostrUtil.createRandomByteArray(16);
assertFalse(Arrays.equals(data1, data2), "Consecutive random arrays should differ");
}
}