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

Moves the SecureRandom related operations out from the Misc class … #236

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
26 changes: 0 additions & 26 deletions src/main/java/com/neovisionaries/ws/client/Misc.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,13 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URI;
import java.security.SecureRandom;
import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


class Misc
{
private static final SecureRandom sRandom = new SecureRandom();


private Misc()
{
}
Expand Down Expand Up @@ -107,28 +103,6 @@ public static String toStringUTF8(byte[] bytes, int offset, int length)
}


/**
* Fill the given buffer with random bytes.
*/
public static byte[] nextBytes(byte[] buffer)
{
sRandom.nextBytes(buffer);

return buffer;
}


/**
* Create a buffer of the given size filled with random bytes.
*/
public static byte[] nextBytes(int nBytes)
{
byte[] buffer = new byte[nBytes];

return nextBytes(buffer);
}


/**
* Convert a WebSocket opcode into a string representation.
*/
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/com/neovisionaries/ws/client/Security.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.neovisionaries.ws.client;

import java.security.SecureRandom;

/**
* WebSocket Security.
*/
public final class Security {

private final SecureRandom sRandom;

private static Security instance;

private Security() {
sRandom = new SecureRandom();
}

/**
* Returns the security instance.
*
* @return security instance.
*/
public static Security getInstance() {
if (instance == null) {
instance = new Security();
}
return instance;
}

/**
* Fill the given buffer with random bytes.
*/
public byte[] nextBytes(byte[] buffer) {
sRandom.nextBytes(buffer);
return buffer;
}

/**
* Create a buffer of the given size filled with random bytes.
*/
public byte[] nextBytes(int nBytes) {
byte[] buffer = new byte[nBytes];
return nextBytes(buffer);
}

}
2 changes: 1 addition & 1 deletion src/main/java/com/neovisionaries/ws/client/WebSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -3395,7 +3395,7 @@ private static String generateWebSocketKey()
byte[] data = new byte[16];

// "randomly selected"
Misc.nextBytes(data);
Security.getInstance().nextBytes(data);

// "base64-encoded"
return Base64.encode(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void write(WebSocketFrame frame) throws IOException
writeFrameExtendedPayloadLength(frame);

// Generate a random masking key.
byte[] maskingKey = Misc.nextBytes(4);
byte[] maskingKey = Security.getInstance().nextBytes(4);

// Write the masking key.
write(maskingKey);
Expand Down