Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Share SecureRandom #1594

Merged
merged 1 commit into from
Aug 5, 2024
Merged

Conversation

bemusementpark
Copy link

Every usage of SecureRandom created a new instance. This is inefficient. This PR adds a shared SecureRandom that is used throughout the app.

Additionally this PR cleans up some other unused usages of SecureRandom, and adds extension functions shuffledSecure, randomSecure and randomSecureOrNull which are all pretty obvious enough if you have a look.

@@ -248,32 +246,6 @@ object Util {
return result
}

fun getSecretBytes(size: Int): ByteArray {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice clean up!
Looking at the files there are a whole bunch more of seemingly unused functions. Might be worth removing those as well if they are truly unused.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed getRandomLengthBytes because it creates a SecureRandom and it's probably better to delete an unused function than maintain it. I didn't want to get too distracted deleting all unused functions in files touched.

@@ -458,12 +459,6 @@ public void setActive(String groupId, boolean active) {
database.update(TABLE_NAME, values, GROUP_ID + " = ?", new String[] {groupId});
}

public byte[] allocateGroupId() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like there are a few other unused functions in there, might be worth cleaning it while we're doing this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto.

public byte[] allocateGroupId() {
    byte[] groupId = new byte[16];
    new SecureRandom().nextBytes(groupId);
    return groupId;
  }

@bemusementpark bemusementpark merged commit 415264e into oxen-io:dev Aug 5, 2024
1 check passed
@bemusementpark bemusementpark deleted the shared-random branch August 5, 2024 03:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants