Completed
Push — master ( 06244e...6f0760 )
by rugk
02:43
created
source/Threema/MsgApi/PublicKeyStores/File.php 1 patch
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -25,7 +25,7 @@  discard block
 block discarded – undo
25 25
 	 * @throws Exception if the file does not exist or not writable
26 26
 	 */
27 27
 	public function __construct($file) {
28
-		if(false === is_writable($file)) {
28
+		if (false === is_writable($file)) {
29 29
 			throw new Exception('file '.$file.' does not exist or is not writable');
30 30
 		}
31 31
 		$this->file = $file;
@@ -39,7 +39,7 @@  discard block
 block discarded – undo
39 39
 	 */
40 40
 	function findPublicKey($threemaId) {
41 41
 		$storeHandle = fopen($this->file, 'r');
42
-		if(false === $storeHandle) {
42
+		if (false === $storeHandle) {
43 43
 			throw new Exception('could not open file '.$this->file);
44 44
 		}
45 45
 		else {
@@ -47,7 +47,7 @@  discard block
 block discarded – undo
47 47
 			$publicKey = null;
48 48
 			while (!feof($storeHandle)) {
49 49
 				$buffer = fgets($storeHandle, 4096);
50
-				if(substr($buffer, 0, 8) == $threemaId) {
50
+				if (substr($buffer, 0, 8) == $threemaId) {
51 51
 					$publicKey = str_replace("\n", '', substr($buffer, 8));
52 52
 					continue;
53 53
 				}
@@ -74,7 +74,7 @@  discard block
 block discarded – undo
74 74
 	 * @return File
75 75
 	 */
76 76
 	public static function create($path) {
77
-		if(false === file_exists($path)) {
77
+		if (false === file_exists($path)) {
78 78
 			//touch
79 79
 			touch($path);
80 80
 		}
Please login to merge, or discard this patch.
source/Threema/MsgApi/Tools/CryptTool.php 1 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/MsgApi/Helpers/E2EHelper.php 1 patch
Spacing   +27 added lines, -27 removed lines patch added patch discarded remove patch
@@ -42,7 +42,7 @@  discard block
 block discarded – undo
42 42
 		$this->cryptTool = $cryptTool;
43 43
 		$this->privateKey = $privateKey;
44 44
 
45
-		if(null === $this->cryptTool) {
45
+		if (null === $this->cryptTool) {
46 46
 			$this->cryptTool = CryptTool::getInstance();
47 47
 		}
48 48
 	}
@@ -84,11 +84,11 @@  discard block
 block discarded – undo
84 84
 		//analyse the file
85 85
 		$fileAnalyzeResult = FileAnalysisTool::analyse($imagePath);
86 86
 
87
-		if(null === $fileAnalyzeResult) {
87
+		if (null === $fileAnalyzeResult) {
88 88
 			throw new Exception('could not analyze the file');
89 89
 		}
90 90
 
91
-		if(false === in_array($fileAnalyzeResult->getMimeType(), array(
91
+		if (false === in_array($fileAnalyzeResult->getMimeType(), array(
92 92
 				'image/jpg',
93 93
 				'image/jpeg',
94 94
 				'image/png' ))) {
@@ -102,9 +102,9 @@  discard block
 block discarded – undo
102 102
 
103 103
 		//encrypt the image file
104 104
 		$encryptionResult = $this->cryptTool->encryptImage(file_get_contents($imagePath), $this->privateKey, $receiverPublicKey);
105
-		$uploadResult =  $this->connection->uploadFile($encryptionResult->getData());
105
+		$uploadResult = $this->connection->uploadFile($encryptionResult->getData());
106 106
 
107
-		if($uploadResult === null || !$uploadResult->isSuccess()) {
107
+		if ($uploadResult === null || !$uploadResult->isSuccess()) {
108 108
 			throw new Exception('could not upload the image ('.$uploadResult->getErrorCode().' '.$uploadResult->getErrorMessage().') '.$uploadResult->getRawResponse());
109 109
 		}
110 110
 
@@ -134,7 +134,7 @@  discard block
 block discarded – undo
134 134
 		//analyse the file
135 135
 		$fileAnalyzeResult = FileAnalysisTool::analyse($filePath);
136 136
 
137
-		if(null === $fileAnalyzeResult) {
137
+		if (null === $fileAnalyzeResult) {
138 138
 			throw new Exception('could not analyze the file');
139 139
 		}
140 140
 
@@ -145,21 +145,21 @@  discard block
 block discarded – undo
145 145
 
146 146
 		//encrypt the main file
147 147
 		$encryptionResult = $this->cryptTool->encryptFile(file_get_contents($filePath));
148
-		$uploadResult =  $this->connection->uploadFile($encryptionResult->getData());
148
+		$uploadResult = $this->connection->uploadFile($encryptionResult->getData());
149 149
 
150
-		if($uploadResult == null || !$uploadResult->isSuccess()) {
150
+		if ($uploadResult == null || !$uploadResult->isSuccess()) {
151 151
 			throw new Exception('could not upload the file ('.$uploadResult->getErrorCode().' '.$uploadResult->getErrorMessage().') '.$uploadResult->getRawResponse());
152 152
 		}
153 153
 
154 154
 		$thumbnailUploadResult = null;
155 155
 
156 156
 		//encrypt the thumbnail file (if exists)
157
-		if(strlen($thumbnailPath) > 0 && true === file_exists($thumbnailPath)) {
157
+		if (strlen($thumbnailPath) > 0 && true === file_exists($thumbnailPath)) {
158 158
 			//encrypt the main file
159 159
 			$thumbnailEncryptionResult = $this->cryptTool->encryptFileThumbnail(file_get_contents($thumbnailPath), $encryptionResult->getKey());
160 160
 			$thumbnailUploadResult = $this->connection->uploadFile($thumbnailEncryptionResult->getData());
161 161
 
162
-			if($thumbnailUploadResult === null || !$thumbnailUploadResult->isSuccess()) {
162
+			if ($thumbnailUploadResult === null || !$thumbnailUploadResult->isSuccess()) {
163 163
 				throw new Exception('could not upload the thumbnail file ('.$thumbnailUploadResult->getErrorCode().' '.$thumbnailUploadResult->getErrorMessage().') '.$thumbnailUploadResult->getRawResponse());
164 164
 			}
165 165
 		}
@@ -205,14 +205,14 @@  discard block
 block discarded – undo
205 205
 										 $outputFolder = null,
206 206
 										 \Closure $downloadMessage = null) {
207 207
 
208
-		if($outputFolder === null || strlen($outputFolder) == 0) {
208
+		if ($outputFolder === null || strlen($outputFolder) == 0) {
209 209
 			$outputFolder = '.';
210 210
 		}
211 211
 
212 212
 		//fetch the public key
213 213
 		$receiverPublicKey = $this->connection->fetchPublicKey($threemaId);
214 214
 
215
-		if(null === $receiverPublicKey || !$receiverPublicKey->isSuccess()) {
215
+		if (null === $receiverPublicKey || !$receiverPublicKey->isSuccess()) {
216 216
 			throw new Exception('Invalid threema id');
217 217
 		}
218 218
 
@@ -223,15 +223,15 @@  discard block
 block discarded – undo
223 223
 			$nonce
224 224
 		);
225 225
 
226
-		if(null === $message || false === is_object($message)) {
226
+		if (null === $message || false === is_object($message)) {
227 227
 			throw new Exception('Could not encrypt box');
228 228
 		}
229 229
 
230 230
 		$receiveResult = new ReceiveMessageResult($messageId, $message);
231 231
 
232
-		if($message instanceof ImageMessage) {
232
+		if ($message instanceof ImageMessage) {
233 233
 			$result = $this->downloadFile($message, $message->getBlobId(), $downloadMessage);
234
-			if(null !== $result && true === $result->isSuccess()) {
234
+			if (null !== $result && true === $result->isSuccess()) {
235 235
 				$image = $this->cryptTool->decryptImage(
236 236
 					$result->getData(),
237 237
 					$this->cryptTool->hex2bin($receiverPublicKey->getPublicKey()),
@@ -243,7 +243,7 @@  discard block
 block discarded – undo
243 243
 					throw new Exception('decryption of image failed');
244 244
 				}
245 245
 				//save file
246
-				$filePath = $outputFolder . '/' . $messageId . '.jpg';
246
+				$filePath = $outputFolder.'/'.$messageId.'.jpg';
247 247
 				$f = fopen($filePath, 'w+');
248 248
 				fwrite($f, $image);
249 249
 				fclose($f);
@@ -251,10 +251,10 @@  discard block
 block discarded – undo
251 251
 				$receiveResult->addFile('image', $filePath);
252 252
 			}
253 253
 		}
254
-		else if($message instanceof FileMessage) {
254
+		else if ($message instanceof FileMessage) {
255 255
 			$result = $this->downloadFile($message, $message->getBlobId(), $downloadMessage);
256 256
 
257
-			if(null !== $result && true === $result->isSuccess()) {
257
+			if (null !== $result && true === $result->isSuccess()) {
258 258
 				$file = $this->cryptTool->decryptFile(
259 259
 					$result->getData(),
260 260
 					hex2bin($message->getEncryptionKey()));
@@ -264,20 +264,20 @@  discard block
 block discarded – undo
264 264
 				}
265 265
 
266 266
 				//save file
267
-				$filePath = $outputFolder . '/' . $messageId . '-' . $message->getFilename();
267
+				$filePath = $outputFolder.'/'.$messageId.'-'.$message->getFilename();
268 268
 				file_put_contents($filePath, $file);
269 269
 
270 270
 				$receiveResult->addFile('file', $filePath);
271 271
 			}
272 272
 
273
-			if(null !== $message->getThumbnailBlobId() && strlen($message->getThumbnailBlobId()) > 0) {
273
+			if (null !== $message->getThumbnailBlobId() && strlen($message->getThumbnailBlobId()) > 0) {
274 274
 				$result = $this->downloadFile($message, $message->getThumbnailBlobId(), $downloadMessage);
275
-				if(null !== $result && true === $result->isSuccess()) {
275
+				if (null !== $result && true === $result->isSuccess()) {
276 276
 					$file = $this->cryptTool->decryptFileThumbnail(
277 277
 						$result->getData(),
278 278
 						$this->cryptTool->hex2bin($message->getEncryptionKey()));
279 279
 
280
-					if(null === $file) {
280
+					if (null === $file) {
281 281
 						throw new Exception('thumbnail decryption failed');
282 282
 					}
283 283
 					//save file
@@ -304,14 +304,14 @@  discard block
 block discarded – undo
304 304
 		//fetch the public key
305 305
 		$receiverPublicKey = $this->connection->fetchPublicKey($threemaId);
306 306
 
307
-		if(null === $receiverPublicKey || !$receiverPublicKey->isSuccess()) {
307
+		if (null === $receiverPublicKey || !$receiverPublicKey->isSuccess()) {
308 308
 			throw new Exception('Invalid threema id');
309 309
 		}
310 310
 
311
-		if(null !== $capabilityCheck) {
311
+		if (null !== $capabilityCheck) {
312 312
 			//check capability
313 313
 			$capability = $this->connection->keyCapability($threemaId);
314
-			if(null === $capability || false === $capabilityCheck->__invoke($capability)) {
314
+			if (null === $capability || false === $capabilityCheck->__invoke($capability)) {
315 315
 				throw new Exception('threema id does not have the capability');
316 316
 			}
317 317
 		}
@@ -327,11 +327,11 @@  discard block
 block discarded – undo
327 327
 	 * @throws Exception
328 328
 	 */
329 329
 	private final function downloadFile(ThreemaMessage $message, $blobId, \Closure $downloadMessage = null) {
330
-		if(null === $downloadMessage
330
+		if (null === $downloadMessage
331 331
 			|| true === $downloadMessage->__invoke($message, $blobId)) {
332 332
 			//make a download
333 333
 			$result = $this->connection->downloadFile($blobId);
334
-			if(null === $result || false === $result->isSuccess()) {
334
+			if (null === $result || false === $result->isSuccess()) {
335 335
 				throw new Exception('could not download the file with blob id '.$blobId);
336 336
 			}
337 337
 
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.