@@ -34,7 +34,7 @@ |
||
| 34 | 34 | * @return string |
| 35 | 35 | */ |
| 36 | 36 | protected function getErrorMessageByErrorCode($httpCode) { |
| 37 | - switch($httpCode) { |
|
| 37 | + switch ($httpCode) { |
|
| 38 | 38 | case 401: |
| 39 | 39 | return 'API identity or secret incorrect or file is empty'; |
| 40 | 40 | case 402: |
@@ -1,8 +1,8 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * @author Threema GmbH |
|
| 4 | - * @copyright Copyright (c) 2015-2016 Threema GmbH |
|
| 5 | - */ |
|
| 3 | + * @author Threema GmbH |
|
| 4 | + * @copyright Copyright (c) 2015-2016 Threema GmbH |
|
| 5 | + */ |
|
| 6 | 6 | |
| 7 | 7 | |
| 8 | 8 | namespace Threema\Core; |
@@ -48,7 +48,7 @@ |
||
| 48 | 48 | * @param object $res |
| 49 | 49 | * @return UploadFileResult |
| 50 | 50 | */ |
| 51 | - function parseResult($httpCode, $res){ |
|
| 51 | + function parseResult($httpCode, $res) { |
|
| 52 | 52 | return new UploadFileResult($httpCode, $res); |
| 53 | 53 | } |
| 54 | 54 | } |
@@ -1,8 +1,8 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * @author Threema GmbH |
|
| 4 | - * @copyright Copyright (c) 2015-2016 Threema GmbH |
|
| 5 | - */ |
|
| 3 | + * @author Threema GmbH |
|
| 4 | + * @copyright Copyright (c) 2015-2016 Threema GmbH |
|
| 5 | + */ |
|
| 6 | 6 | |
| 7 | 7 | |
| 8 | 8 | namespace Threema\Core; |
@@ -48,7 +48,7 @@ |
||
| 48 | 48 | * @param object $res |
| 49 | 49 | * @return UploadFileResult |
| 50 | 50 | */ |
| 51 | - function parseResult($httpCode, $res){ |
|
| 51 | + function parseResult($httpCode, $res) { |
|
| 52 | 52 | return new UploadFileResult($httpCode, $res); |
| 53 | 53 | } |
| 54 | 54 | } |
@@ -1,8 +1,8 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * @author Threema GmbH |
|
| 4 | - * @copyright Copyright (c) 2015-2016 Threema GmbH |
|
| 5 | - */ |
|
| 3 | + * @author Threema GmbH |
|
| 4 | + * @copyright Copyright (c) 2015-2016 Threema GmbH |
|
| 5 | + */ |
|
| 6 | 6 | |
| 7 | 7 | |
| 8 | 8 | namespace Threema\Core; |
@@ -124,7 +124,7 @@ |
||
| 124 | 124 | * @throws Exception |
| 125 | 125 | */ |
| 126 | 126 | public function validate() { |
| 127 | - if(false === $this->isSupported()) { |
|
| 127 | + if (false === $this->isSupported()) { |
|
| 128 | 128 | throw new Exception('Sodium implementation not supported'); |
| 129 | 129 | } |
| 130 | 130 | return true; |
@@ -1,8 +1,8 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * @author Threema GmbH |
|
| 4 | - * @copyright Copyright (c) 2015-2016 Threema GmbH |
|
| 5 | - */ |
|
| 3 | + * @author Threema GmbH |
|
| 4 | + * @copyright Copyright (c) 2015-2016 Threema GmbH |
|
| 5 | + */ |
|
| 6 | 6 | |
| 7 | 7 | |
| 8 | 8 | namespace Threema\Core; |
@@ -145,6 +145,11 @@ discard block |
||
| 145 | 145 | return $this->makeBox($message, $nonce, $senderPrivateKey, $recipientPublicKey); |
| 146 | 146 | } |
| 147 | 147 | |
| 148 | + /** |
|
| 149 | + * @param string $senderPrivateKey |
|
| 150 | + * @param string $recipientPublicKey |
|
| 151 | + * @param string $nonce |
|
| 152 | + */ |
|
| 148 | 153 | final public function encryptFileMessage(UploadFileResult $uploadFileResult, |
| 149 | 154 | EncryptResult $encryptResult, |
| 150 | 155 | UploadFileResult $thumbnailUploadFileResult = null, |
@@ -193,9 +198,9 @@ discard block |
||
| 193 | 198 | * make a secret box |
| 194 | 199 | * |
| 195 | 200 | * @param $data |
| 196 | - * @param $nonce |
|
| 201 | + * @param string $nonce |
|
| 197 | 202 | * @param $key |
| 198 | - * @return mixed |
|
| 203 | + * @return string |
|
| 199 | 204 | */ |
| 200 | 205 | abstract protected function makeSecretBox($data, $nonce, $key); |
| 201 | 206 | |
@@ -337,6 +342,9 @@ discard block |
||
| 337 | 342 | return hash_hmac('sha256', $phoneNoClean, self::PHONENO_HMAC_KEY); |
| 338 | 343 | } |
| 339 | 344 | |
| 345 | + /** |
|
| 346 | + * @return string |
|
| 347 | + */ |
|
| 340 | 348 | abstract protected function createRandom($size); |
| 341 | 349 | |
| 342 | 350 | /** |
@@ -379,7 +387,7 @@ discard block |
||
| 379 | 387 | abstract public function validate(); |
| 380 | 388 | |
| 381 | 389 | /** |
| 382 | - * @param $data |
|
| 390 | + * @param string $data |
|
| 383 | 391 | * @return EncryptResult |
| 384 | 392 | */ |
| 385 | 393 | public final function encryptFile($data) { |
@@ -391,7 +399,7 @@ discard block |
||
| 391 | 399 | /** |
| 392 | 400 | * @param string $data as binary |
| 393 | 401 | * @param string $key as binary |
| 394 | - * @return null|string |
|
| 402 | + * @return string |
|
| 395 | 403 | */ |
| 396 | 404 | public final function decryptFile($data, $key) { |
| 397 | 405 | $result = $this->openSecretBox($data, self::FILE_NONCE, $key); |
@@ -408,6 +416,10 @@ discard block |
||
| 408 | 416 | return new EncryptResult($box, $key, self::FILE_THUMBNAIL_NONCE, strlen($box)); |
| 409 | 417 | } |
| 410 | 418 | |
| 419 | + /** |
|
| 420 | + * @param string $data |
|
| 421 | + * @param string $key |
|
| 422 | + */ |
|
| 411 | 423 | public final function decryptFileThumbnail($data, $key) { |
| 412 | 424 | $result = $this->openSecretBox($data, self::FILE_THUMBNAIL_NONCE, $key); |
| 413 | 425 | return false === $result ? null : $result; |
@@ -42,8 +42,8 @@ discard block |
||
| 42 | 42 | * @return CryptTool |
| 43 | 43 | */ |
| 44 | 44 | public static function getInstance() { |
| 45 | - if(null === self::$instance) { |
|
| 46 | - foreach(array( |
|
| 45 | + if (null === self::$instance) { |
|
| 46 | + foreach (array( |
|
| 47 | 47 | function() { |
| 48 | 48 | return self::createInstance(self::TYPE_SODIUM); |
| 49 | 49 | }, |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | return self::createInstance(self::TYPE_SALT); |
| 52 | 52 | }) as $instanceGenerator) { |
| 53 | 53 | $i = $instanceGenerator->__invoke(); |
| 54 | - if(null !== $i) { |
|
| 54 | + if (null !== $i) { |
|
| 55 | 55 | self::$instance = $i; |
| 56 | 56 | break; |
| 57 | 57 | } |
@@ -66,18 +66,18 @@ discard block |
||
| 66 | 66 | * @return null|CryptTool null on unknown type |
| 67 | 67 | */ |
| 68 | 68 | public static function createInstance($type) { |
| 69 | - switch($type) { |
|
| 69 | + switch ($type) { |
|
| 70 | 70 | case self::TYPE_SODIUM: |
| 71 | 71 | $instance = new CryptToolSodium(); |
| 72 | - if(false === $instance->isSupported()) { |
|
| 72 | + if (false === $instance->isSupported()) { |
|
| 73 | 73 | //try to instance old version of sodium wrapper |
| 74 | 74 | /** @noinspection PhpDeprecationInspection */ |
| 75 | 75 | $instance = new CryptToolSodiumDep(); |
| 76 | 76 | } |
| 77 | - return $instance->isSupported() ? $instance :null; |
|
| 77 | + return $instance->isSupported() ? $instance : null; |
|
| 78 | 78 | case self::TYPE_SALT: |
| 79 | 79 | $instance = new CryptToolSalt(); |
| 80 | - return $instance->isSupported() ? $instance :null; |
|
| 80 | + return $instance->isSupported() ? $instance : null; |
|
| 81 | 81 | default: |
| 82 | 82 | return null; |
| 83 | 83 | } |
@@ -104,7 +104,7 @@ discard block |
||
| 104 | 104 | */ |
| 105 | 105 | final public function encryptMessageText($text, $senderPrivateKey, $recipientPublicKey, $nonce) { |
| 106 | 106 | /* prepend type byte (0x01) to message data */ |
| 107 | - $textBytes = "\x01" . $text; |
|
| 107 | + $textBytes = "\x01".$text; |
|
| 108 | 108 | |
| 109 | 109 | /* determine random amount of PKCS7 padding */ |
| 110 | 110 | $padbytes = $this->generatePadBytes(); |
@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | $senderPrivateKey, |
| 130 | 130 | $recipientPublicKey, |
| 131 | 131 | $nonce) { |
| 132 | - $message = "\x02" . hex2bin($uploadFileResult->getBlobId()); |
|
| 132 | + $message = "\x02".hex2bin($uploadFileResult->getBlobId()); |
|
| 133 | 133 | $message .= pack('V', $encryptResult->getSize()); |
| 134 | 134 | $message .= $encryptResult->getNonce(); |
| 135 | 135 | |
@@ -160,11 +160,11 @@ discard block |
||
| 160 | 160 | 'i' => 0 |
| 161 | 161 | ); |
| 162 | 162 | |
| 163 | - if($thumbnailUploadFileResult != null && strlen($thumbnailUploadFileResult->getBlobId()) > 0) { |
|
| 163 | + if ($thumbnailUploadFileResult != null && strlen($thumbnailUploadFileResult->getBlobId()) > 0) { |
|
| 164 | 164 | $messageContent['t'] = $thumbnailUploadFileResult->getBlobId(); |
| 165 | 165 | } |
| 166 | 166 | |
| 167 | - $message = "\x17" . json_encode($messageContent); |
|
| 167 | + $message = "\x17".json_encode($messageContent); |
|
| 168 | 168 | |
| 169 | 169 | /* determine random amount of PKCS7 padding */ |
| 170 | 170 | $padbytes = $this->generatePadBytes(); |
@@ -237,7 +237,7 @@ discard block |
||
| 237 | 237 | |
| 238 | 238 | /* remove padding */ |
| 239 | 239 | $padbytes = ord($data[strlen($data)-1]); |
| 240 | - $realDataLength = strlen($data) - $padbytes; |
|
| 240 | + $realDataLength = strlen($data)-$padbytes; |
|
| 241 | 241 | if ($realDataLength < 1) { |
| 242 | 242 | throw new BadMessageException(); |
| 243 | 243 | } |
@@ -263,7 +263,7 @@ discard block |
||
| 263 | 263 | return new TextMessage(substr($data, 1)); |
| 264 | 264 | case DeliveryReceipt::TYPE_CODE: |
| 265 | 265 | /* Delivery receipt */ |
| 266 | - if ($realDataLength < (self::MESSAGE_ID_LEN-2) || (($realDataLength - 2) % self::MESSAGE_ID_LEN) != 0) { |
|
| 266 | + if ($realDataLength < (self::MESSAGE_ID_LEN-2) || (($realDataLength-2)%self::MESSAGE_ID_LEN) != 0) { |
|
| 267 | 267 | throw new BadMessageException(); |
| 268 | 268 | } |
| 269 | 269 | |
@@ -273,7 +273,7 @@ discard block |
||
| 273 | 273 | return new DeliveryReceipt($receiptType, $messageIds); |
| 274 | 274 | case ImageMessage::TYPE_CODE: |
| 275 | 275 | /* Image Message */ |
| 276 | - if ($realDataLength != 1 + self::BLOB_ID_LEN + self::IMAGE_FILE_SIZE_LEN + self::IMAGE_NONCE_LEN) { |
|
| 276 | + if ($realDataLength != 1+self::BLOB_ID_LEN+self::IMAGE_FILE_SIZE_LEN+self::IMAGE_NONCE_LEN) { |
|
| 277 | 277 | throw new BadMessageException(); |
| 278 | 278 | } |
| 279 | 279 | |
@@ -284,12 +284,12 @@ discard block |
||
| 284 | 284 | case FileMessage::TYPE_CODE: |
| 285 | 285 | /* Image Message */ |
| 286 | 286 | $decodeResult = json_decode(substr($data, 1), true); |
| 287 | - if(null === $decodeResult || false === $decodeResult) { |
|
| 287 | + if (null === $decodeResult || false === $decodeResult) { |
|
| 288 | 288 | throw new BadMessageException(); |
| 289 | 289 | } |
| 290 | 290 | |
| 291 | 291 | $values = AssocArray::byJsonString(substr($data, 1), array('b', 't', 'k', 'm', 'n', 's')); |
| 292 | - if(null === $values) { |
|
| 292 | + if (null === $values) { |
|
| 293 | 293 | throw new BadMessageException(); |
| 294 | 294 | } |
| 295 | 295 | |
@@ -391,7 +391,7 @@ discard block |
||
| 391 | 391 | * @return null|string |
| 392 | 392 | */ |
| 393 | 393 | public final function decryptFile($data, $key) { |
| 394 | - $result = $this->openSecretBox($data, self::FILE_NONCE, $key); |
|
| 394 | + $result = $this->openSecretBox($data, self::FILE_NONCE, $key); |
|
| 395 | 395 | return false === $result ? null : $result; |
| 396 | 396 | } |
| 397 | 397 | |
@@ -402,7 +402,7 @@ discard block |
||
| 402 | 402 | */ |
| 403 | 403 | public final function encryptFileThumbnail($data, $key) { |
| 404 | 404 | $box = $this->makeSecretBox($data, self::FILE_THUMBNAIL_NONCE, $key); |
| 405 | - return new EncryptResult($box, $key, self::FILE_THUMBNAIL_NONCE, strlen($box)); |
|
| 405 | + return new EncryptResult($box, $key, self::FILE_THUMBNAIL_NONCE, strlen($box)); |
|
| 406 | 406 | } |
| 407 | 407 | |
| 408 | 408 | public final function decryptFileThumbnail($data, $key) { |
@@ -449,7 +449,7 @@ discard block |
||
| 449 | 449 | */ |
| 450 | 450 | private function generatePadBytes() { |
| 451 | 451 | $padbytes = 0; |
| 452 | - while($padbytes < 1 || $padbytes > 255) { |
|
| 452 | + while ($padbytes < 1 || $padbytes > 255) { |
|
| 453 | 453 | $padbytes = ord($this->createRandom(1)); |
| 454 | 454 | } |
| 455 | 455 | return $padbytes; |
@@ -1,8 +1,8 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * @author Threema GmbH |
|
| 4 | - * @copyright Copyright (c) 2015-2016 Threema GmbH |
|
| 5 | - */ |
|
| 3 | + * @author Threema GmbH |
|
| 4 | + * @copyright Copyright (c) 2015-2016 Threema GmbH |
|
| 5 | + */ |
|
| 6 | 6 | |
| 7 | 7 | |
| 8 | 8 | namespace Threema\Core; |
@@ -1,8 +1,8 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * @author Threema GmbH |
|
| 4 | - * @copyright Copyright (c) 2015-2016 Threema GmbH |
|
| 5 | - */ |
|
| 3 | + * @author Threema GmbH |
|
| 4 | + * @copyright Copyright (c) 2015-2016 Threema GmbH |
|
| 5 | + */ |
|
| 6 | 6 | |
| 7 | 7 | |
| 8 | 8 | namespace Threema\Core; |
@@ -1,8 +1,8 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * @author Threema GmbH |
|
| 4 | - * @copyright Copyright (c) 2015-2016 Threema GmbH |
|
| 5 | - */ |
|
| 3 | + * @author Threema GmbH |
|
| 4 | + * @copyright Copyright (c) 2015-2016 Threema GmbH |
|
| 5 | + */ |
|
| 6 | 6 | |
| 7 | 7 | |
| 8 | 8 | namespace Threema\Core; |
@@ -1,8 +1,8 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * @author Threema GmbH |
|
| 4 | - * @copyright Copyright (c) 2015-2016 Threema GmbH |
|
| 5 | - */ |
|
| 3 | + * @author Threema GmbH |
|
| 4 | + * @copyright Copyright (c) 2015-2016 Threema GmbH |
|
| 5 | + */ |
|
| 6 | 6 | |
| 7 | 7 | |
| 8 | 8 | namespace Threema\Core; |
@@ -1,8 +1,8 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * @author Threema GmbH |
|
| 4 | - * @copyright Copyright (c) 2015-2016 Threema GmbH |
|
| 5 | - */ |
|
| 3 | + * @author Threema GmbH |
|
| 4 | + * @copyright Copyright (c) 2015-2016 Threema GmbH |
|
| 5 | + */ |
|
| 6 | 6 | |
| 7 | 7 | |
| 8 | 8 | namespace Threema\Core; |