Completed
Pull Request — master (#44)
by rugk
02:40
created
source/Threema/MsgApi/Tools/CryptTool.php 3 patches
Doc Comments   +15 added lines, -3 removed lines patch added patch discarded remove patch
@@ -142,6 +142,11 @@  discard block
 block discarded – undo
142 142
 		return $this->makeBox($message, $nonce, $senderPrivateKey, $recipientPublicKey);
143 143
 	}
144 144
 
145
+	/**
146
+	 * @param string $senderPrivateKey
147
+	 * @param string $recipientPublicKey
148
+	 * @param string $nonce
149
+	 */
145 150
 	final public function encryptFileMessage(UploadFileResult $uploadFileResult,
146 151
 											 EncryptResult $encryptResult,
147 152
 											 UploadFileResult $thumbnailUploadFileResult = null,
@@ -190,9 +195,9 @@  discard block
 block discarded – undo
190 195
 	 * make a secret box
191 196
 	 *
192 197
 	 * @param $data
193
-	 * @param $nonce
198
+	 * @param string $nonce
194 199
 	 * @param $key
195
-	 * @return mixed
200
+	 * @return string
196 201
 	 */
197 202
 	abstract protected function makeSecretBox($data, $nonce, $key);
198 203
 
@@ -334,6 +339,9 @@  discard block
 block discarded – undo
334 339
 		return hash_hmac('sha256', $phoneNoClean, self::PHONENO_HMAC_KEY);
335 340
 	}
336 341
 
342
+	/**
343
+	 * @return string
344
+	 */
337 345
 	abstract protected function createRandom($size);
338 346
 
339 347
 	/**
@@ -376,7 +384,7 @@  discard block
 block discarded – undo
376 384
 	abstract public function validate();
377 385
 
378 386
 	/**
379
-	 * @param $data
387
+	 * @param string $data
380 388
 	 * @return EncryptResult
381 389
 	 */
382 390
 	public final function encryptFile($data) {
@@ -405,6 +413,10 @@  discard block
 block discarded – undo
405 413
 		return new EncryptResult($box, $key,  self::FILE_THUMBNAIL_NONCE, strlen($box));
406 414
 	}
407 415
 
416
+	/**
417
+	 * @param string $data
418
+	 * @param string $key
419
+	 */
408 420
 	public final function decryptFileThumbnail($data, $key) {
409 421
 		$result = $this->openSecretBox($data, self::FILE_THUMBNAIL_NONCE, $key);
410 422
 		return false === $result ? null : $result;
Please login to merge, or discard this patch.
Indentation   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -524,8 +524,8 @@
 block discarded – undo
524 524
 			# partly taken from https://github.com/symfony/polyfill-php56/blob/master/Php56.php#L45-L51
525 525
 			$ret = 0;
526 526
 			for ($i = 0; $i < strlen($str1); ++$i) {
527
-	            $ret |= ord($str1[$i]) ^ ord($str2[$i]);
528
-	        }
527
+				$ret |= ord($str1[$i]) ^ ord($str2[$i]);
528
+			}
529 529
 			return 0 === $result;
530 530
 		}
531 531
 	}
Please login to merge, or discard this patch.
Spacing   +19 added lines, -19 removed lines patch added patch discarded remove patch
@@ -42,8 +42,8 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
129 129
 			$senderPrivateKey,
130 130
 			$recipientPublicKey,
131 131
 			$nonce) {
132
-		$message = "\x02" . $this->hex2bin($uploadFileResult->getBlobId());
132
+		$message = "\x02".$this->hex2bin($uploadFileResult->getBlobId());
133 133
 		$message .= pack('V', $encryptResult->getSize());
134 134
 		$message .= $encryptResult->getNonce();
135 135
 
@@ -160,11 +160,11 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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;
Please login to merge, or discard this patch.
source/Threema/Console/Command/GenerateKeyPair.php 1 patch
Unused Use Statements   -1 removed lines patch added patch discarded remove patch
@@ -8,7 +8,6 @@
 block discarded – undo
8 8
 namespace Threema\Console\Command;
9 9
 
10 10
 use Threema\Console\Common;
11
-use Threema\MsgApi\Tools\CryptTool;
12 11
 
13 12
 class GenerateKeyPair extends Base {
14 13
 	function __construct() {
Please login to merge, or discard this patch.
source/Threema/MsgApi/Tests/CryptToolTest.php 1 patch
Spacing   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -56,7 +56,7 @@  discard block
 block discarded – undo
56 56
 
57 57
 			$this->assertNotNull($message);
58 58
 			$this->assertTrue($message instanceof TextMessage);
59
-			if($message instanceof TextMessage) {
59
+			if ($message instanceof TextMessage) {
60 60
 				$this->assertEquals($message->getText(), 'Dies ist eine Testnachricht. äöü');
61 61
 			}
62 62
 		});
@@ -92,7 +92,7 @@  discard block
 block discarded – undo
92 92
 
93 93
 
94 94
 	public function testDerivePublicKey() {
95
-		$this->doTest(function(CryptTool $cryptTool, $prefix){
95
+		$this->doTest(function(CryptTool $cryptTool, $prefix) {
96 96
 			$publicKey = $cryptTool->derivePublicKey($cryptTool->hex2bin(Common::getPrivateKey(Constants::myPrivateKey)));
97 97
 			$myPublicKey = $cryptTool->hex2bin(Common::getPublicKey(Constants::myPublicKey));
98 98
 
@@ -165,15 +165,15 @@  discard block
 block discarded – undo
165 165
 
166 166
 				// test different strings when comparing and get time needed
167 167
 				$result = [];
168
-				foreach(array(
169
-					'length' => [$string1, $string1 . 'a'],
168
+				foreach (array(
169
+					'length' => [$string1, $string1.'a'],
170 170
 					'diff' => [$string1, $string2],
171 171
 					'same' => [$string1, $string1]
172 172
 				) as $testName => $strings) {
173 173
 					$timeStart = microtime(true);
174 174
 					$comparisonResult = $cryptTool->stringCompare($strings[0], $strings[1]);
175 175
 					$timeEnd = microtime(true);
176
-					$timeElapsed = $timeEnd - $timeStart;
176
+					$timeElapsed = $timeEnd-$timeStart;
177 177
 
178 178
 					// echo $prefix.': '.$humanDescr[$testName].': '.$timeElapsed.'; result: '.$comparisonResult.PHP_EOL;
179 179
 					$result[$testName] = [$timeElapsed, $comparisonResult];
@@ -188,8 +188,8 @@  discard block
 block discarded – undo
188 188
 
189 189
 				// check timings
190 190
 				echo 'Timing test results with '.$prefix.':'.PHP_EOL;
191
-				$timingRatio = $result['diff'][0] / $result['same'][0];
192
-				$absoluteDifference = abs($result['diff'][0] - $result['same'][0]);
191
+				$timingRatio = $result['diff'][0]/$result['same'][0];
192
+				$absoluteDifference = abs($result['diff'][0]-$result['same'][0]);
193 193
 				echo 'timing ratio: '.$timingRatio.PHP_EOL;
194 194
 				echo 'absolute difference: '.$absoluteDifference.PHP_EOL;
195 195
 
@@ -208,7 +208,7 @@  discard block
 block discarded – undo
208 208
 	 */
209 209
 	public function testRemoveVar() {
210 210
 		$this->doTest(function(CryptTool $cryptTool, $prefix) {
211
-			foreach(array(
211
+			foreach (array(
212 212
 						'hex' => Constants::myPrivateKeyExtract,
213 213
 						'bin' => $cryptTool->hex2bin(Constants::myPrivateKeyExtract)
214 214
 					) as $key => $testVar) {
@@ -222,12 +222,12 @@  discard block
 block discarded – undo
222 222
 	}
223 223
 
224 224
 	private function doTest(\Closure $c) {
225
-		foreach(array(
225
+		foreach (array(
226 226
 					'Salt' => CryptTool::createInstance(CryptTool::TYPE_SALT),
227 227
 					'Sodium' => CryptTool::createInstance(CryptTool::TYPE_SODIUM)
228 228
 				) as $key => $instance) {
229 229
 
230
-			if($instance === null) {
230
+			if ($instance === null) {
231 231
 				echo $key.": could not instance crypt tool\n";
232 232
 				break;
233 233
 			}
Please login to merge, or discard this patch.
source/Threema/MsgApi/Connection.php 1 patch
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -202,12 +202,12 @@  discard block
 block discarded – undo
202 202
 			}
203 203
 		}
204 204
 		if ($tlsCipher = $this->setting->getTlsOption(ConnectionSettings::tlsOptionCipher, null)) {
205
-			if(true === is_string($tlsCipher)) {
205
+			if (true === is_string($tlsCipher)) {
206 206
 				$options[CURLOPT_SSL_CIPHER_LIST] = $tlsCipher;
207 207
 			}
208 208
 		}
209 209
 		if ($pinnedKey = $this->setting->getTlsOption(ConnectionSettings::tlsOptionPinnedKey, Constants::DEFAULT_PINNED_KEY)) {
210
-			if(true === is_string($pinnedKey)) {
210
+			if (true === is_string($pinnedKey)) {
211 211
 				$options[CURLOPT_PINNEDPUBLICKEY] = $pinnedKey;
212 212
 			}
213 213
 		}
@@ -239,7 +239,7 @@  discard block
 block discarded – undo
239 239
 		return $this->call($command->getPath(),
240 240
 			$this->createDefaultOptions($progress),
241 241
 			$params,
242
-			function ($httpCode, $response) use ($command) {
242
+			function($httpCode, $response) use ($command) {
243 243
 				return $command->parseResult($httpCode, $response);
244 244
 			});
245 245
 	}
@@ -257,7 +257,7 @@  discard block
 block discarded – undo
257 257
 		$options[CURLOPT_HTTPHEADER] = array(
258 258
 			'Content-Type: application/x-www-form-urlencoded');
259 259
 
260
-		return $this->call($command->getPath(), $options, null, function ($httpCode, $response) use ($command) {
260
+		return $this->call($command->getPath(), $options, null, function($httpCode, $response) use ($command) {
261 261
 			return $command->parseResult($httpCode, $response);
262 262
 		});
263 263
 	}
@@ -277,7 +277,7 @@  discard block
 block discarded – undo
277 277
 			'blob' => $command->getData()
278 278
 		);
279 279
 
280
-		return $this->call($command->getPath(), $options, $params, function ($httpCode, $response) use ($command) {
280
+		return $this->call($command->getPath(), $options, $params, function($httpCode, $response) use ($command) {
281 281
 			return $command->parseResult($httpCode, $response);
282 282
 		});
283 283
 	}
@@ -304,7 +304,7 @@  discard block
 block discarded – undo
304 304
 
305 305
 		$response = curl_exec($session);
306 306
 		if (false === $response) {
307
-			throw new Exception($path . ' ' . curl_error($session));
307
+			throw new Exception($path.' '.curl_error($session));
308 308
 		}
309 309
 
310 310
 		$httpCode = curl_getinfo($session, CURLINFO_HTTP_CODE);
Please login to merge, or discard this patch.
source/Threema/MsgApi/ConnectionSettings.php 1 patch
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -49,20 +49,20 @@
 block discarded – undo
49 49
 		$this->host = $host;
50 50
 
51 51
 		// TLS options
52
-		if(null !== $tlsOptions && is_array($tlsOptions)) {
53
-			if(true === array_key_exists(self::tlsOptionForceHttps, $tlsOptions)) {
52
+		if (null !== $tlsOptions && is_array($tlsOptions)) {
53
+			if (true === array_key_exists(self::tlsOptionForceHttps, $tlsOptions)) {
54 54
 				$this->tlsOptions[self::tlsOptionForceHttps] = $tlsOptions[self::tlsOptionForceHttps] === true;
55 55
 			}
56 56
 
57
-			if(true === array_key_exists(self::tlsOptionVersion, $tlsOptions)) {
57
+			if (true === array_key_exists(self::tlsOptionVersion, $tlsOptions)) {
58 58
 				$this->tlsOptions[self::tlsOptionVersion] = $tlsOptions[self::tlsOptionVersion];
59 59
 			}
60 60
 
61
-			if(true === array_key_exists(self::tlsOptionCipher, $tlsOptions)) {
61
+			if (true === array_key_exists(self::tlsOptionCipher, $tlsOptions)) {
62 62
 				$this->tlsOptions[self::tlsOptionCipher] = $tlsOptions[self::tlsOptionCipher];
63 63
 			}
64 64
 
65
-			if(true === array_key_exists(self::tlsOptionPinnedKey, $tlsOptions)) {
65
+			if (true === array_key_exists(self::tlsOptionPinnedKey, $tlsOptions)) {
66 66
 				$this->tlsOptions[self::tlsOptionPinnedKey] = $tlsOptions[self::tlsOptionPinnedKey];
67 67
 			}
68 68
 		}
Please login to merge, or discard this patch.
source/bootstrap.php 1 patch
Spacing   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -2,11 +2,11 @@  discard block
 block discarded – undo
2 2
 require_once("Salt/autoload.php");
3 3
 
4 4
 //define possibly missing constants
5
-defined('CURL_SSLVERSION_DEFAULT')  || define('CURL_SSLVERSION_DEFAULT', 0);
6
-defined('CURL_SSLVERSION_TLSv1')    || define('CURL_SSLVERSION_TLSv1', 1);
7
-defined('CURL_SSLVERSION_TLSv1_1')  || define('CURL_SSLVERSION_TLSv1_1', 5);
8
-defined('CURL_SSLVERSION_TLSv1_2')  || define('CURL_SSLVERSION_TLSv1_2', 6);
9
-defined('CURLOPT_PINNEDPUBLICKEY')  || define('CURLOPT_PINNEDPUBLICKEY', 10230);
5
+defined('CURL_SSLVERSION_DEFAULT') || define('CURL_SSLVERSION_DEFAULT', 0);
6
+defined('CURL_SSLVERSION_TLSv1') || define('CURL_SSLVERSION_TLSv1', 1);
7
+defined('CURL_SSLVERSION_TLSv1_1') || define('CURL_SSLVERSION_TLSv1_1', 5);
8
+defined('CURL_SSLVERSION_TLSv1_2') || define('CURL_SSLVERSION_TLSv1_2', 6);
9
+defined('CURLOPT_PINNEDPUBLICKEY') || define('CURLOPT_PINNEDPUBLICKEY', 10230);
10 10
 
11 11
 //define autoloader
12 12
 $d = dirname(__FILE__);
@@ -16,12 +16,12 @@  discard block
 block discarded – undo
16 16
 	$fileName  = '';
17 17
 	if ($lastNsPos = strrpos($className, '\\')) {
18 18
 		$namespace = substr($className, 0, $lastNsPos);
19
-		$className = substr($className, $lastNsPos + 1);
20
-		$fileName  = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
19
+		$className = substr($className, $lastNsPos+1);
20
+		$fileName  = str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR;
21 21
 	}
22
-	$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
22
+	$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
23 23
 
24
-	if(true === file_exists( $d.'/'.$fileName)) {
24
+	if (true === file_exists($d.'/'.$fileName)) {
25 25
 		require $d.'/'.$fileName;
26 26
 	}
27 27
 });
@@ -30,7 +30,7 @@  discard block
 block discarded – undo
30 30
 define('MSGAPI_SDK_VERSION', $sdkVersion);
31 31
 $cryptTool = Threema\MsgApi\Tools\CryptTool::getInstance();
32 32
 
33
-if(null === $cryptTool) {
33
+if (null === $cryptTool) {
34 34
 	throw new \Threema\Core\Exception("no supported crypt-tool");
35 35
 }
36 36
 
Please login to merge, or discard this patch.