@@ -101,6 +101,7 @@ public function setFile($data = '')
101101 while (! feof ($ fp )) {
102102 yield (array ) unpack ('C* ' , (string ) fread ($ fp , 4096 ));
103103 }
104+
104105 fclose ($ fp );
105106 unset($ fp );
106107 });
@@ -305,6 +306,7 @@ public function output(?string $filePath = null)
305306
306307 $ OLE2 = new OLE_PPS_File (OLE ::Asc2Ucs ('EncryptedPackage ' ));
307308 $ OLE2 ->init ();
309+
308310 $ filesize = (int ) filesize ($ encryptedPackage ['tmpFile ' ]);
309311
310312 for ($ i = 0 ; $ i < ($ filesize / 4096 ); $ i ++) {
@@ -324,7 +326,7 @@ public function output(?string $filePath = null)
324326
325327 $ root ->save ($ filePath );
326328
327- return file_get_contents ($ filePath );
329+ return ( string ) file_get_contents ($ filePath );
328330 }
329331
330332 /**
@@ -427,21 +429,18 @@ private function arrayToXml(array $array = [])
427429 /**
428430 * Crypt method
429431 *
430- * @param string $cipherAlgorithm
431- * @param string $cipherChaining
432432 * @param array $key
433- * @param bool $encrypt
434433 * @param array<int|string, int|string> $iv
435434 * @param array<int|string, int|string> $input
436435 */
437- private function _crypt ($ encrypt , $ cipherAlgorithm , $ cipherChaining , $ key , $ iv , $ input )
436+ private function _crypt (bool $ encrypt , string $ cipherAlgorithm , string $ cipherChaining , $ key , $ iv , $ input )
438437 {
439438 $ algorithm = $ cipherAlgorithm . '- ' . (count ($ key ) * 8 );
440439
441440 if ($ cipherChaining === 'ChainingModeCBC ' ) {
442441 $ algorithm .= '-cbc ' ;
443442 } else {
444- throw new Exception (" Unknown cipher chaining: { $ cipherChaining}" );
443+ throw new Exception (' Unknown cipher chaining: ' . $ cipherChaining );
445444 }
446445
447446 $ cipher = [];
@@ -463,11 +462,9 @@ private function _crypt($encrypt, $cipherAlgorithm, $cipherChaining, $key, $iv,
463462 /**
464463 * Hashing
465464 *
466- * @param string $algorithm
467- *
468465 * @return array<int|string, int|string>
469466 */
470- private function _hash ($ algorithm , ...$ buffers )
467+ private function _hash (string $ algorithm , ...$ buffers )
471468 {
472469 try {
473470 $ ctx = hash_init ($ algorithm );
@@ -489,7 +486,7 @@ private function _hash($algorithm, ...$buffers)
489486 *
490487 * @return array<int|string, int|string>
491488 */
492- private function _hmac ($ algorithm , $ key , $ fileName )
489+ private function _hmac (string $ algorithm , $ key , string $ fileName )
493490 {
494491 return (array ) unpack ('C* ' , hash_hmac_file (
495492 $ algorithm ,
@@ -501,19 +498,16 @@ private function _hmac($algorithm, $key, $fileName)
501498
502499 /**
503500 * Create Unsigned Integer 32-bit Buffer
504- *
505- * @param int $value
506- * @param int $bufferSize
507501 */
508- private function _createUInt32LEBuffer ($ value , $ bufferSize = 4 )
502+ private function _createUInt32LEBuffer (int $ value , int $ bufferSize = 4 )
509503 {
510504 return array_pad (array_values ((array ) unpack ('C* ' , pack ('V ' , $ value ))), $ bufferSize , 0 );
511505 }
512506
513- private function _convertPasswordToKey ($ password , $ hashAlgorithm , $ saltValue , $ spinCount , $ keyBits , $ blockKey )
507+ private function _convertPasswordToKey (string $ password , string $ hashAlgorithm , $ saltValue , int $ spinCount , int $ keyBits , $ blockKey )
514508 {
515509 // Password must be in unicode buffer
516- $ passwordBuffer = array_map (' hexdec ' , str_split (bin2hex (mb_convert_encoding ($ password , 'UTF-16LE ' , 'utf-8 ' )), 2 ));
510+ $ passwordBuffer = array_map (hexdec (...) , str_split (bin2hex (mb_convert_encoding ($ password , 'UTF-16LE ' , 'utf-8 ' )), 2 ));
517511
518512 // Generate the initial hash
519513 $ key = $ this ->_hash ($ hashAlgorithm , $ saltValue , $ passwordBuffer );
@@ -547,12 +541,10 @@ private function _convertPasswordToKey($password, $hashAlgorithm, $saltValue, $s
547541 /**
548542 * Create initialization vector
549543 *
550- * @param string $hashAlgorithm
551544 * @param array<int|string, int|string> $saltValue
552- * @param int $blockSize
553545 * @param mixed $blockKey
554546 */
555- private function _createIV ($ hashAlgorithm , $ saltValue , $ blockSize , $ blockKey )
547+ private function _createIV (string $ hashAlgorithm , $ saltValue , int $ blockSize , $ blockKey )
556548 {
557549 // Create the block key from the current index
558550 if (is_int ($ blockKey )) {
@@ -574,40 +566,38 @@ private function _createIV($hashAlgorithm, $saltValue, $blockSize, $blockKey)
574566 /**
575567 * Package Crypt
576568 *
577- * @param bool $encrypt
578- * @param string $cipherAlgorithm
579- * @param string $cipherChaining
580- * @param string $hashAlgorithm
581- * @param int $blockSize
582569 * @param array<int|string, int|string> $saltValue
583- * @param mixed $input
570+ * @param Closure|null $input
584571 * @param array<int|string, int|string> $key
585572 */
586573 private function _cryptPackage (
587- $ encrypt ,
588- $ cipherAlgorithm ,
589- $ cipherChaining ,
590- $ hashAlgorithm ,
591- $ blockSize ,
574+ bool $ encrypt ,
575+ string $ cipherAlgorithm ,
576+ string $ cipherChaining ,
577+ string $ hashAlgorithm ,
578+ int $ blockSize ,
592579 $ saltValue ,
593580 $ input ,
594581 $ key = [],
595582 ) {
596- $ tmpOutputChunk = (string ) tempnam (sys_get_temp_dir (), 'outputChunk ' );
597- $ tmpFileHeaderLength = (string ) tempnam (sys_get_temp_dir (), 'fileHeaderLength ' );
598- $ tmpFile = (string ) tempnam (sys_get_temp_dir (), 'file ' );
583+ $ sysTempDir = sys_get_temp_dir ();
584+ $ tmpOutputChunk = (string ) tempnam ($ sysTempDir , 'outputChunk ' );
585+ $ tmpFileHeaderLength = (string ) tempnam ($ sysTempDir , 'fileHeaderLength ' );
586+ $ tmpFile = (string ) tempnam ($ sysTempDir , 'file ' );
599587
600- if (is_callable ( $ input) && is_a ($ in = $ input (), 'Generator ' )) {
588+ if ($ input !== null && is_a ($ in = $ input (), 'Generator ' )) {
601589 $ inputCount = 0 ;
602590
603591 foreach ($ in as $ i => $ inputChunk ) {
604592 $ lengthInputChunk = count ($ inputChunk );
605593 // Grab the next chunk
606594 $ inputCount += $ lengthInputChunk ;
607595 $ remainder = $ lengthInputChunk % $ blockSize ;
596+
608597 if ($ remainder !== 0 ) {
609598 $ inputChunk = array_pad ($ inputChunk , $ lengthInputChunk + (16 - $ remainder ), 0 );
610599 }
600+
611601 // Create the initialization vector
612602 $ iv = $ this ->_createIV ($ hashAlgorithm , $ saltValue , $ blockSize , $ i );
613603
@@ -632,6 +622,8 @@ private function _cryptPackage(
632622 'tmpFile ' => $ tmpFile ,
633623 ];
634624 }
625+
626+ return null ;
635627 }
636628
637629 /**
@@ -657,6 +649,7 @@ private function build(SimpleXMLElement $rootNode, array $data = [])
657649 $ rootNode ->addAttribute ($ kk , $ vv );
658650 }
659651 }
652+
660653 if ($ k === 'children ' ) {
661654 $ isNamespace = count (explode (': ' , $ vv ['name ' ])) === 2 ;
662655 $ r = $ isNamespace ? $ rootNode ->addChild ('xmlns: ' . $ vv ['name ' ], '' ) : $ rootNode ->addChild ($ vv ['name ' ], '' );
0 commit comments