Skip to content

Commit

Permalink
Remove RandomUtils
Browse files Browse the repository at this point in the history
We can use again the one from Apache. They added the methods back.
  • Loading branch information
zapek committed Jan 18, 2025
1 parent dd062c8 commit 4555908
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 172 deletions.
12 changes: 6 additions & 6 deletions app/src/test/java/io/xeres/app/crypto/aead/AEADTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 by David Gerber - https://zapek.com
* Copyright (c) 2024-2025 by David Gerber - https://zapek.com
*
* This file is part of Xeres.
*
Expand All @@ -19,8 +19,8 @@

package io.xeres.app.crypto.aead;

import io.xeres.testutils.RandomUtils;
import io.xeres.testutils.TestUtils;
import org.apache.commons.lang3.RandomUtils;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -48,9 +48,9 @@ void Instance_ThrowsException() throws NoSuchMethodException
@Test
void EncryptChaCha20Poly1305_DecryptChaCha20Poly1305_Success()
{
var nonce = RandomUtils.nextBytes(12);
var nonce = RandomUtils.insecure().randomBytes(12);
var plainText = "hello world".getBytes(StandardCharsets.UTF_8);
var aad = RandomUtils.nextBytes(16);
var aad = RandomUtils.insecure().randomBytes(16);

var cipherText = AEAD.encryptChaCha20Poly1305(key, nonce, plainText, aad);
var decryptedText = AEAD.decryptChaCha20Poly1305(key, nonce, cipherText, aad);
Expand All @@ -61,9 +61,9 @@ void EncryptChaCha20Poly1305_DecryptChaCha20Poly1305_Success()
@Test
void EncryptChaCha20Aes256_DecryptChaCha20Aes256_Success()
{
var nonce = RandomUtils.nextBytes(12);
var nonce = RandomUtils.insecure().randomBytes(12);
var plainText = "hello world".getBytes(StandardCharsets.UTF_8);
var aad = RandomUtils.nextBytes(16);
var aad = RandomUtils.insecure().randomBytes(16);

var cipherText = AEAD.encryptChaCha20Sha256(key, nonce, plainText, aad);
var decryptedText = AEAD.decryptChaCha20Sha256(key, nonce, cipherText, aad);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2023 by David Gerber - https://zapek.com
* Copyright (c) 2019-2025 by David Gerber - https://zapek.com
*
* This file is part of Xeres.
*
Expand Down Expand Up @@ -34,12 +34,12 @@ private ChatRoomFakes()

public static ChatRoom createChatRoomEntity()
{
return createChatRoomEntity(IdFakes.createLong(), IdentityFakes.createOwn(), RandomStringUtils.randomAlphabetic(8), RandomStringUtils.randomAlphabetic(8), 0);
return createChatRoomEntity(IdFakes.createLong(), IdentityFakes.createOwn(), RandomStringUtils.insecure().nextAlphabetic(8), RandomStringUtils.insecure().nextAlphabetic(8), 0);
}

public static ChatRoom createChatRoomEntity(IdentityGroupItem identityGroupItem)
{
return createChatRoomEntity(IdFakes.createLong(), identityGroupItem, RandomStringUtils.randomAlphabetic(8), RandomStringUtils.randomAlphabetic(8), 0);
return createChatRoomEntity(IdFakes.createLong(), identityGroupItem, RandomStringUtils.insecure().nextAlphabetic(8), RandomStringUtils.insecure().nextAlphabetic(8), 0);
}

public static ChatRoom createChatRoomEntity(long roomId, IdentityGroupItem identityGroupItem, String name, String topic, int flags)
Expand All @@ -49,7 +49,7 @@ public static ChatRoom createChatRoomEntity(long roomId, IdentityGroupItem ident

public static io.xeres.app.xrs.service.chat.ChatRoom createChatRoom()
{
return createChatRoom(IdFakes.createLong(), RandomStringUtils.randomAlphabetic(8), RandomStringUtils.randomAlphabetic(8), RoomType.PUBLIC, 5, false);
return createChatRoom(IdFakes.createLong(), RandomStringUtils.insecure().nextAlphabetic(8), RandomStringUtils.insecure().nextAlphabetic(8), RoomType.PUBLIC, 5, false);
}

public static io.xeres.app.xrs.service.chat.ChatRoom createChatRoom(long id, String name, String topic, RoomType roomType, int userCount, boolean isSigned)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2023 by David Gerber - https://zapek.com
* Copyright (c) 2019-2025 by David Gerber - https://zapek.com
*
* This file is part of Xeres.
*
Expand Down Expand Up @@ -35,15 +35,15 @@ private ForumGroupItemFakes()

public static ForumGroupItem createForumGroupItem()
{
return createForumGroupItem(IdFakes.createGxsId(), RandomStringUtils.randomAlphabetic(8));
return createForumGroupItem(IdFakes.createGxsId(), RandomStringUtils.insecure().nextAlphabetic(8));
}

public static ForumGroupItem createForumGroupItem(GxsId gxsId, String name)
{
var item = new ForumGroupItem(gxsId, name);
item.setDiffusionFlags(EnumSet.noneOf(GxsPrivacyFlags.class));
item.setSignatureFlags(EnumSet.noneOf(GxsSignatureFlags.class));
item.setDescription(RandomStringUtils.randomAlphabetic(8));
item.setDescription(RandomStringUtils.insecure().nextAlphabetic(8));
return item;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 by David Gerber - https://zapek.com
* Copyright (c) 2023-2025 by David Gerber - https://zapek.com
*
* This file is part of Xeres.
*
Expand Down Expand Up @@ -34,7 +34,7 @@ private ForumMessageItemFakes()

public static ForumMessageItem createForumMessageItem()
{
return createForumMessageItem(IdFakes.createGxsId(), IdFakes.createMessageId(), RandomStringUtils.randomAlphabetic(8));
return createForumMessageItem(IdFakes.createGxsId(), IdFakes.createMessageId(), RandomStringUtils.insecure().nextAlphabetic(8));
}

private static ForumMessageItem createForumMessageItem(GxsId gxsId, MessageId messageId, String name)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2023 by David Gerber - https://zapek.com
* Copyright (c) 2019-2025 by David Gerber - https://zapek.com
*
* This file is part of Xeres.
*
Expand Down Expand Up @@ -36,7 +36,7 @@ private IdentityGroupItemFakes()

public static IdentityGroupItem createIdentityGroupItem()
{
return createIdentityGroupItem(IdFakes.createGxsId(), RandomStringUtils.randomAlphabetic(8));
return createIdentityGroupItem(IdFakes.createGxsId(), RandomStringUtils.insecure().nextAlphabetic(8));
}

public static IdentityGroupItem createIdentityGroupItem(GxsId gxsId, String name)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2023 by David Gerber - https://zapek.com
* Copyright (c) 2019-2025 by David Gerber - https://zapek.com
*
* This file is part of Xeres.
*
Expand Down Expand Up @@ -30,7 +30,7 @@
import io.xeres.common.id.LocationIdentifier;
import io.xeres.common.id.MessageId;
import io.xeres.testutils.IdFakes;
import io.xeres.testutils.RandomUtils;
import org.apache.commons.lang3.RandomUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
Expand Down Expand Up @@ -444,7 +444,7 @@ void Serialize_TlvString()
void Serialize_TlvKeySignature()
{
var buf = Unpooled.buffer();
var key = RandomUtils.nextBytes(30);
var key = RandomUtils.insecure().randomBytes(30);

var input = new Signature(IdFakes.createGxsId(), key);

Expand All @@ -464,7 +464,7 @@ void Serialize_TlvKeySignatureSet()
var buf = Unpooled.buffer();
Set<Signature> input = new HashSet<>();
var gxsId = IdFakes.createGxsId();
var signature = RandomUtils.nextBytes(20);
var signature = RandomUtils.insecure().randomBytes(20);
var keySignature = new Signature(Signature.Type.ADMIN, gxsId, signature);
input.add(keySignature);

Expand Down Expand Up @@ -533,8 +533,8 @@ void Serialize_TlvSet_GxsId()
void Serialize_TlvSet_MessageId()
{
var buf = Unpooled.buffer();
var messageId1 = new MessageId(RandomUtils.nextBytes(MessageId.LENGTH));
var messageId2 = new MessageId(RandomUtils.nextBytes(MessageId.LENGTH));
var messageId1 = new MessageId(RandomUtils.insecure().randomBytes(MessageId.LENGTH));
var messageId2 = new MessageId(RandomUtils.insecure().randomBytes(MessageId.LENGTH));
Set<MessageId> input = new HashSet<>();
input.add(messageId1);
input.add(messageId2);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
/*
* Copyright (c) 2025 by David Gerber - https://zapek.com
*
* This file is part of Xeres.
*
* Xeres is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Xeres is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Xeres. If not, see <http://www.gnu.org/licenses/>.
*/

package io.xeres.app.xrs.service.filetransfer;

import io.xeres.testutils.RandomUtils;
import org.apache.commons.lang3.RandomUtils;
import org.junit.jupiter.api.Test;

import java.io.File;
Expand All @@ -18,7 +37,7 @@ private static File createTempFile(int size) throws IOException
var tempFile = Files.createTempFile("fileseeder", ".tmp").toFile();
if (size > 0)
{
Files.write(tempFile.toPath(), RandomUtils.nextBytes(size));
Files.write(tempFile.toPath(), RandomUtils.insecure().randomBytes(size));
}
return tempFile;
}
Expand Down
13 changes: 7 additions & 6 deletions common/src/testFixtures/java/io/xeres/testutils/IdFakes.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 by David Gerber - https://zapek.com
* Copyright (c) 2023-2025 by David Gerber - https://zapek.com
*
* This file is part of Xeres.
*
Expand All @@ -22,6 +22,7 @@
import io.xeres.common.id.GxsId;
import io.xeres.common.id.LocationIdentifier;
import io.xeres.common.id.MessageId;
import org.apache.commons.lang3.RandomUtils;

public final class IdFakes
{
Expand All @@ -32,7 +33,7 @@ private IdFakes()

public static GxsId createGxsId()
{
return new GxsId(RandomUtils.nextBytes(GxsId.LENGTH));
return new GxsId(RandomUtils.insecure().randomBytes(GxsId.LENGTH));
}

public static GxsId createGxsId(byte[] gxsId)
Expand All @@ -42,21 +43,21 @@ public static GxsId createGxsId(byte[] gxsId)

public static MessageId createMessageId()
{
return new MessageId(RandomUtils.nextBytes(MessageId.LENGTH));
return new MessageId(RandomUtils.insecure().randomBytes(MessageId.LENGTH));
}

public static LocationIdentifier createLocationIdentifier()
{
return new LocationIdentifier(RandomUtils.nextBytes(LocationIdentifier.LENGTH));
return new LocationIdentifier(RandomUtils.insecure().randomBytes(LocationIdentifier.LENGTH));
}

public static long createLong()
{
return RandomUtils.nextLong(1, Long.MAX_VALUE);
return RandomUtils.insecure().randomLong(1, Long.MAX_VALUE);
}

public static int createInt()
{
return RandomUtils.nextInt(1, Integer.MAX_VALUE);
return RandomUtils.insecure().randomInt(1, Integer.MAX_VALUE);
}
}
139 changes: 0 additions & 139 deletions common/src/testFixtures/java/io/xeres/testutils/RandomUtils.java

This file was deleted.

Loading

0 comments on commit 4555908

Please sign in to comment.