@@ -12,6 +12,7 @@ final class File
12
12
* @param string $inputFilename
13
13
* @param string $outputFilename
14
14
* @param Key $key
15
+ * @return void
15
16
*
16
17
* @throws Ex\EnvironmentIsBrokenException
17
18
* @throws Ex\IOException
@@ -32,6 +33,7 @@ public static function encryptFile($inputFilename, $outputFilename, Key $key)
32
33
* @param string $inputFilename
33
34
* @param string $outputFilename
34
35
* @param string $password
36
+ * @return void
35
37
*
36
38
* @throws Ex\EnvironmentIsBrokenException
37
39
* @throws Ex\IOException
@@ -51,6 +53,7 @@ public static function encryptFileWithPassword($inputFilename, $outputFilename,
51
53
* @param string $inputFilename
52
54
* @param string $outputFilename
53
55
* @param Key $key
56
+ * @return void
54
57
*
55
58
* @throws Ex\EnvironmentIsBrokenException
56
59
* @throws Ex\IOException
@@ -72,6 +75,7 @@ public static function decryptFile($inputFilename, $outputFilename, Key $key)
72
75
* @param string $inputFilename
73
76
* @param string $outputFilename
74
77
* @param string $password
78
+ * @return void
75
79
*
76
80
* @throws Ex\EnvironmentIsBrokenException
77
81
* @throws Ex\IOException
@@ -93,6 +97,7 @@ public static function decryptFileWithPassword($inputFilename, $outputFilename,
93
97
* @param resource $inputHandle
94
98
* @param resource $outputHandle
95
99
* @param Key $key
100
+ * @return void
96
101
*
97
102
* @throws Ex\EnvironmentIsBrokenException
98
103
* @throws Ex\WrongKeyOrModifiedCiphertextException
@@ -114,6 +119,7 @@ public static function encryptResource($inputHandle, $outputHandle, Key $key)
114
119
* @param resource $inputHandle
115
120
* @param resource $outputHandle
116
121
* @param string $password
122
+ * @return void
117
123
*
118
124
* @throws Ex\EnvironmentIsBrokenException
119
125
* @throws Ex\IOException
@@ -135,6 +141,7 @@ public static function encryptResourceWithPassword($inputHandle, $outputHandle,
135
141
* @param resource $inputHandle
136
142
* @param resource $outputHandle
137
143
* @param Key $key
144
+ * @return void
138
145
*
139
146
* @throws Ex\EnvironmentIsBrokenException
140
147
* @throws Ex\IOException
@@ -156,6 +163,7 @@ public static function decryptResource($inputHandle, $outputHandle, Key $key)
156
163
* @param resource $inputHandle
157
164
* @param resource $outputHandle
158
165
* @param string $password
166
+ * @return void
159
167
*
160
168
* @throws Ex\EnvironmentIsBrokenException
161
169
* @throws Ex\IOException
@@ -176,6 +184,7 @@ public static function decryptResourceWithPassword($inputHandle, $outputHandle,
176
184
* @param string $inputFilename
177
185
* @param string $outputFilename
178
186
* @param KeyOrPassword $secret
187
+ * @return void
179
188
*
180
189
* @throws Ex\CryptoException
181
190
* @throws Ex\IOException
@@ -240,6 +249,7 @@ private static function encryptFileInternal($inputFilename, $outputFilename, Key
240
249
* @param string $inputFilename
241
250
* @param string $outputFilename
242
251
* @param KeyOrPassword $secret
252
+ * @return void
243
253
*
244
254
* @throws Ex\CryptoException
245
255
* @throws Ex\IOException
@@ -254,7 +264,7 @@ private static function decryptFileInternal($inputFilename, $outputFilename, Key
254
264
self ::getLastErrorMessage ()
255
265
);
256
266
}
257
-
267
+
258
268
if (\is_callable ('\\stream_set_read_buffer ' )) {
259
269
/* This call can fail, but the only consequence is performance. */
260
270
\stream_set_read_buffer ($ if , 0 );
@@ -269,7 +279,7 @@ private static function decryptFileInternal($inputFilename, $outputFilename, Key
269
279
self ::getLastErrorMessage ()
270
280
);
271
281
}
272
-
282
+
273
283
if (\is_callable ('\\stream_set_write_buffer ' )) {
274
284
/* This call can fail, but the only consequence is performance. */
275
285
\stream_set_write_buffer ($ of , 0 );
@@ -306,6 +316,7 @@ private static function decryptFileInternal($inputFilename, $outputFilename, Key
306
316
* @param resource $inputHandle
307
317
* @param resource $outputHandle
308
318
* @param KeyOrPassword $secret
319
+ * @return void
309
320
*
310
321
* @throws Ex\EnvironmentIsBrokenException
311
322
* @throws Ex\IOException
@@ -335,8 +346,9 @@ private static function encryptResourceInternal($inputHandle, $outputHandle, Key
335
346
$ iv = Core::secureRandom ($ ivsize );
336
347
337
348
/* Initialize a streaming HMAC state. */
349
+ /** @var resource $hmac */
338
350
$ hmac = \hash_init (Core::HASH_FUNCTION_NAME , HASH_HMAC , $ akey );
339
- if ($ hmac === false ) {
351
+ if (! \is_resource ( $ hmac) ) {
340
352
throw new Ex \EnvironmentIsBrokenException (
341
353
'Cannot initialize a hash context '
342
354
);
@@ -358,14 +370,15 @@ private static function encryptResourceInternal($inputHandle, $outputHandle, Key
358
370
$ thisIv = $ iv ;
359
371
360
372
/* How many blocks do we encrypt at a time? We increment by this value. */
361
- $ inc = Core::BUFFER_BYTE_SIZE / Core::BLOCK_BYTE_SIZE ;
373
+ $ inc = ( int ) ( Core::BUFFER_BYTE_SIZE / Core::BLOCK_BYTE_SIZE ) ;
362
374
363
375
/* Loop until we reach the end of the input file. */
364
376
$ at_file_end = false ;
365
377
while (! (\feof ($ inputHandle ) || $ at_file_end )) {
366
378
/* Find out if we can read a full buffer, or only a partial one. */
379
+ /** @var int */
367
380
$ pos = \ftell ($ inputHandle );
368
- if ($ pos === false ) {
381
+ if (! \is_int ( $ pos) ) {
369
382
throw new Ex \IOException (
370
383
'Could not get current position in input file during encryption '
371
384
);
@@ -385,6 +398,7 @@ private static function encryptResourceInternal($inputHandle, $outputHandle, Key
385
398
}
386
399
387
400
/* Encrypt this buffer. */
401
+ /** @var string */
388
402
$ encrypted = \openssl_encrypt (
389
403
$ read ,
390
404
Core::CIPHER_METHOD ,
@@ -393,7 +407,7 @@ private static function encryptResourceInternal($inputHandle, $outputHandle, Key
393
407
$ thisIv
394
408
);
395
409
396
- if ($ encrypted === false ) {
410
+ if (! \is_string ( $ encrypted) ) {
397
411
throw new Ex \EnvironmentIsBrokenException (
398
412
'OpenSSL encryption error '
399
413
);
@@ -422,6 +436,7 @@ private static function encryptResourceInternal($inputHandle, $outputHandle, Key
422
436
* @param resource $inputHandle
423
437
* @param resource $outputHandle
424
438
* @param KeyOrPassword $secret
439
+ * @return void
425
440
*
426
441
* @throws Ex\EnvironmentIsBrokenException
427
442
* @throws Ex\IOException
@@ -476,7 +491,7 @@ public static function decryptResourceInternal($inputHandle, $outputHandle, KeyO
476
491
$ thisIv = $ iv ;
477
492
478
493
/* How many blocks do we encrypt at a time? We increment by this value. */
479
- $ inc = Core::BUFFER_BYTE_SIZE / Core::BLOCK_BYTE_SIZE ;
494
+ $ inc = ( int ) ( Core::BUFFER_BYTE_SIZE / Core::BLOCK_BYTE_SIZE ) ;
480
495
481
496
/* Get the HMAC. */
482
497
if (\fseek ($ inputHandle , (-1 * Core::MAC_BYTE_SIZE ), SEEK_END ) === false ) {
@@ -486,8 +501,9 @@ public static function decryptResourceInternal($inputHandle, $outputHandle, KeyO
486
501
}
487
502
488
503
/* Get the position of the last byte in the actual ciphertext. */
504
+ /** @var int $cipher_end */
489
505
$ cipher_end = \ftell ($ inputHandle );
490
- if ($ cipher_end === false ) {
506
+ if (! \is_int ( $ cipher_end) ) {
491
507
throw new Ex \IOException (
492
508
'Cannot read input file '
493
509
);
@@ -496,11 +512,13 @@ public static function decryptResourceInternal($inputHandle, $outputHandle, KeyO
496
512
--$ cipher_end ;
497
513
498
514
/* Read the HMAC. */
515
+ /** @var string $stored_mac */
499
516
$ stored_mac = self ::readBytes ($ inputHandle , Core::MAC_BYTE_SIZE );
500
517
501
518
/* Initialize a streaming HMAC state. */
519
+ /** @var resource $hmac */
502
520
$ hmac = \hash_init (Core::HASH_FUNCTION_NAME , HASH_HMAC , $ akey );
503
- if ($ hmac === false ) {
521
+ if (! \is_resource ( $ hmac) ) {
504
522
throw new Ex \EnvironmentIsBrokenException (
505
523
'Cannot initialize a hash context '
506
524
);
@@ -525,12 +543,14 @@ public static function decryptResourceInternal($inputHandle, $outputHandle, KeyO
525
543
\hash_update ($ hmac , $ header );
526
544
\hash_update ($ hmac , $ file_salt );
527
545
\hash_update ($ hmac , $ iv );
546
+ /** @var resource $hmac2 */
528
547
$ hmac2 = \hash_copy ($ hmac );
529
548
530
549
$ break = false ;
531
550
while (! $ break ) {
551
+ /** @var int $pos */
532
552
$ pos = \ftell ($ inputHandle );
533
- if ($ pos === false ) {
553
+ if (! \is_int ( $ pos) ) {
534
554
throw new Ex \IOException (
535
555
'Could not get current position in input file during decryption '
536
556
);
@@ -554,8 +574,9 @@ public static function decryptResourceInternal($inputHandle, $outputHandle, KeyO
554
574
\hash_update ($ hmac , $ read );
555
575
556
576
/* Remember this buffer-sized chunk's HMAC. */
577
+ /** @var resource $chunk_mac */
557
578
$ chunk_mac = \hash_copy ($ hmac );
558
- if ($ chunk_mac === false ) {
579
+ if (! \is_resource ( $ chunk_mac) ) {
559
580
throw new Ex \EnvironmentIsBrokenException (
560
581
'Cannot duplicate a hash context '
561
582
);
@@ -564,6 +585,7 @@ public static function decryptResourceInternal($inputHandle, $outputHandle, KeyO
564
585
}
565
586
566
587
/* Get the final HMAC, which should match the stored one. */
588
+ /** @var string $final_mac */
567
589
$ final_mac = \hash_final ($ hmac , true );
568
590
569
591
/* Verify the HMAC. */
@@ -584,8 +606,9 @@ public static function decryptResourceInternal($inputHandle, $outputHandle, KeyO
584
606
585
607
$ at_file_end = false ;
586
608
while (! $ at_file_end ) {
609
+ /** @var int $pos */
587
610
$ pos = \ftell ($ inputHandle );
588
- if ($ pos === false ) {
611
+ if (! \is_int ( $ pos) ) {
589
612
throw new Ex \IOException (
590
613
'Could not get current position in input file during decryption '
591
614
);
@@ -609,8 +632,9 @@ public static function decryptResourceInternal($inputHandle, $outputHandle, KeyO
609
632
* remembered from pass #1 to ensure attackers didn't change the
610
633
* ciphertext after MAC verification. */
611
634
\hash_update ($ hmac2 , $ read );
635
+ /** @var resource $calc_mac */
612
636
$ calc_mac = \hash_copy ($ hmac2 );
613
- if ($ calc_mac === false ) {
637
+ if (! \is_resource ( $ calc_mac) ) {
614
638
throw new Ex \EnvironmentIsBrokenException (
615
639
'Cannot duplicate a hash context '
616
640
);
@@ -628,14 +652,15 @@ public static function decryptResourceInternal($inputHandle, $outputHandle, KeyO
628
652
}
629
653
630
654
/* Decrypt this buffer-sized chunk. */
655
+ /** @var string $decrypted */
631
656
$ decrypted = \openssl_decrypt (
632
657
$ read ,
633
658
Core::CIPHER_METHOD ,
634
659
$ ekey ,
635
660
OPENSSL_RAW_DATA ,
636
661
$ thisIv
637
662
);
638
- if ($ decrypted === false ) {
663
+ if (! \is_string ( $ decrypted) ) {
639
664
throw new Ex \EnvironmentIsBrokenException (
640
665
'OpenSSL decryption error '
641
666
);
@@ -649,6 +674,7 @@ public static function decryptResourceInternal($inputHandle, $outputHandle, KeyO
649
674
);
650
675
651
676
/* Increment the IV by the amount of blocks in a buffer. */
677
+ /** @var string $thisIv */
652
678
$ thisIv = Core::incrementCounter ($ thisIv , $ inc );
653
679
/* WARNING: Usually, unless the file is a multiple of the buffer
654
680
* size, $thisIv will contain an incorrect value here on the last
@@ -661,6 +687,7 @@ public static function decryptResourceInternal($inputHandle, $outputHandle, KeyO
661
687
*
662
688
* @param resource $stream
663
689
* @param int $num_bytes
690
+ * @return string
664
691
*
665
692
* @throws Ex\IOException
666
693
* @throws Ex\EnvironmentIsBrokenException
@@ -679,9 +706,9 @@ public static function readBytes($stream, $num_bytes)
679
706
$ buf = '' ;
680
707
$ remaining = $ num_bytes ;
681
708
while ($ remaining > 0 && ! \feof ($ stream )) {
709
+ /** @var string $read */
682
710
$ read = \fread ($ stream , $ remaining );
683
-
684
- if ($ read === false ) {
711
+ if (!\is_string ($ read )) {
685
712
throw new Ex \IOException (
686
713
'Could not read from the file '
687
714
);
@@ -703,6 +730,7 @@ public static function readBytes($stream, $num_bytes)
703
730
* @param resource $stream
704
731
* @param string $buf
705
732
* @param int $num_bytes
733
+ * @return int
706
734
*
707
735
* @throws Ex\IOException
708
736
*
@@ -726,13 +754,14 @@ public static function writeBytes($stream, $buf, $num_bytes = null)
726
754
}
727
755
$ remaining = $ num_bytes ;
728
756
while ($ remaining > 0 ) {
757
+ /** @var int $written */
729
758
$ written = \fwrite ($ stream , $ buf , $ remaining );
730
- if ($ written === false ) {
759
+ if (! \is_int ( $ written) ) {
731
760
throw new Ex \IOException (
732
761
'Could not write to the file '
733
762
);
734
763
}
735
- $ buf = Core::ourSubstr ($ buf , $ written , null );
764
+ $ buf = ( string ) Core::ourSubstr ($ buf , $ written , null );
736
765
$ remaining -= $ written ;
737
766
}
738
767
return $ num_bytes ;
0 commit comments