/** * 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); }
/** * Generate a value for Sec-WebSocket-Key. * * <blockquote> * <p><i> * The request MUST include a header field with the name Sec-WebSocket-Key. * The value of this header field MUST be a nonce consisting of a randomly * selected 16-byte value that has been base64-encoded (see Section 4 of * RFC 4648). The nonce MUST be selected randomly for each connection. * </i></p> * </blockquote> * * @return * A randomly generated WebSocket key. */ private static String generateWebSocketKey() { // "16-byte value" byte[] data = new byte[16]; // "randomly selected" Misc.nextBytes(data); // "base64-encoded" return Base64.encode(data); }
public void write(WebSocketFrame frame) throws IOException { writeFrame0(frame); writeFrame1(frame); writeFrameExtendedPayloadLength(frame); // Generate a random masking key. byte[] maskingKey = Misc.nextBytes(4); // Write the masking key. write(maskingKey); // Write the payload. writeFramePayload(frame, maskingKey); }
/** * 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); }
/** * Generate a value for Sec-WebSocket-Key. * * <blockquote> * <p><i> * The request MUST include a header field with the name Sec-WebSocket-Key. * The value of this header field MUST be a nonce consisting of a randomly * selected 16-byte value that has been base64-encoded (see Section 4 of * RFC 4648). The nonce MUST be selected randomly for each connection. * </i></p> * </blockquote> * * @return * A randomly generated WebSocket key. */ private static String generateWebSocketKey() { // "16-byte value" byte[] data = new byte[16]; // "randomly selected" Misc.nextBytes(data); // "base64-encoded" return Base64.encode(data); }
public void write(WebSocketFrame frame) throws IOException { writeFrame0(frame); writeFrame1(frame); writeFrameExtendedPayloadLength(frame); // Generate a random masking key. byte[] maskingKey = Misc.nextBytes(4); // Write the masking key. write(maskingKey); // Write the payload. writeFramePayload(frame, maskingKey); }