aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIvan Enderlin <ivan.enderlin@hoa-project.net>2016-05-24 08:54:15 +0200
committerIvan Enderlin <ivan.enderlin@hoa-project.net>2016-06-17 17:04:44 +0200
commit7fabd70e744d0613abd1ca15a312066f03491046 (patch)
tree3f88aa7808be02838a0b2426d4bcc4e962651c48
parent093def40a93ad4a77eeb5c91e5fbda38dad69420 (diff)
downloadWebsocket-7fabd70e744d0613abd1ca15a312066f03491046.zip
Websocket-7fabd70e744d0613abd1ca15a312066f03491046.tar.gz
Websocket-7fabd70e744d0613abd1ca15a312066f03491046.tar.bz2
Protocol: Extract the `getMaskingKey` method.
This is easier for testing and it makes sense to have it as a single method.
-rw-r--r--Protocol/Rfc6455.php40
1 files changed, 26 insertions, 14 deletions
diff --git a/Protocol/Rfc6455.php b/Protocol/Rfc6455.php
index 050825b..2182bb1 100644
--- a/Protocol/Rfc6455.php
+++ b/Protocol/Rfc6455.php
@@ -256,20 +256,7 @@ class Rfc6455 extends Generic
if (0x0 === $mask) {
$out .= $message;
} else {
- $maskingKey = [];
-
- if (function_exists('openssl_random_pseudo_bytes')) {
- $maskingKey = array_map(
- 'ord',
- str_split(
- openssl_random_pseudo_bytes(4)
- )
- );
- } else {
- for ($i = 0; $i < 4; ++$i) {
- $maskingKey[] = mt_rand(1, 255);
- }
- }
+ $maskingKey = $this->getMaskingKey();
for ($i = 0, $max = strlen($message); $i < $max; ++$i) {
$message[$i] = chr(ord($message[$i]) ^ $maskingKey[$i % 4]);
@@ -284,6 +271,31 @@ class Rfc6455 extends Generic
}
/**
+ * Get a random masking key.
+ *
+ * @return array
+ */
+ public function getMaskingKey()
+ {
+ if (true === function_exists('openssl_random_pseudo_bytes')) {
+ $maskingKey = array_map(
+ 'ord',
+ str_split(
+ openssl_random_pseudo_bytes(4)
+ )
+ );
+ } else {
+ $maskingKey = [];
+
+ for ($i = 0; $i < 4; ++$i) {
+ $maskingKey[] = mt_rand(1, 255);
+ }
+ }
+
+ return $maskingKey;
+ }
+
+ /**
* Send a message.
*
* @param string $message Message.