22
33namespace Rcsofttech85 \FileHandler ;
44
5+ use Rcsofttech85 \FileHandler \Exception \FileHandlerException ;
56use Rcsofttech85 \FileHandler \Exception \HashException ;
67use Rcsofttech85 \FileHandler \Validator \FileValidatorTrait ;
78
89class FileHashChecker
910{
1011 use FileValidatorTrait;
1112
13+
1214 public const ALGO_256 = 'sha3-256 ' ;
1315 public const ALGO_512 = 'sha3-512 ' ;
1416
17+ private const SEARCH_COLUMN_NAME = 'File ' ;
18+ private const SEARCH_COLUMN_VALUE = 'Hash ' ;
19+
20+
1521 /**
16- * @param string $filename
1722 * @param CsvFileHandler $csvFileHandler
18- * @throws Exception\FileHandlerException
1923 */
20- public function __construct (private string $ filename , private readonly CsvFileHandler $ csvFileHandler )
24+ public function __construct (private readonly CsvFileHandler $ csvFileHandler )
2125 {
22- $ this ->filename = $ this ->validateFileName ($ filename );
2326 }
2427
2528 /**
26- * @param string $storedHashesFile
29+ * @param string $filename
2730 * @param string $algo
2831 * @return bool
29- * @throws Exception\ FileHandlerException
32+ * @throws FileHandlerException
3033 * @throws HashException
3134 */
3235
33- public function verifyHash (string $ storedHashesFile , string $ algo = self ::ALGO_256 ): bool
36+ public function verifyHash (string $ filename , string $ algo = self ::ALGO_256 ): bool
3437 {
35- if (!$ storedHashesFile ) {
36- throw new HashException ('file not found ' );
37- }
38-
38+ $ storedHashesFile = $ this ->getParameter (self ::STORED_HASH_FILE );
3939 $ file = $ this ->csvFileHandler ->searchInCsvFile (
4040 filename: $ storedHashesFile ,
41- keyword: $ this -> filename ,
42- column: ' File ' ,
41+ keyword: $ filename ,
42+ column: self :: SEARCH_COLUMN_NAME ,
4343 format: FileHandler::ARRAY_FORMAT
4444 );
4545
46+
4647 if (!$ file || !is_array ($ file )) {
4748 throw new HashException ('this file is not hashed ' );
4849 }
4950
5051 $ expectedHash = $ file ['Hash ' ];
51- $ hash = $ this ->hashFile ($ algo );
52+ $ hash = $ this ->hashFile ($ filename , $ algo );
53+
5254
5355 if ($ hash !== $ expectedHash ) {
5456 return false ;
@@ -58,16 +60,65 @@ public function verifyHash(string $storedHashesFile, string $algo = self::ALGO_2
5860 }
5961
6062 /**
63+ * @param string $filename
6164 * @param string $algo
6265 * @return string
63- * @throws HashException
66+ * @throws HashException|FileHandlerException
6467 */
6568
66- public function hashFile (string $ algo = self ::ALGO_256 ): string
69+ public function hashFile (string $ filename , string $ algo = self ::ALGO_256 ): string
6770 {
71+ $ this ->validateFileName ($ filename );
6872 if (!in_array ($ algo , [self ::ALGO_512 , self ::ALGO_256 ])) {
6973 throw new HashException ('algorithm not supported ' );
7074 }
71- return hash_file ($ algo , $ this ->filename );
75+
76+ if (!$ hash = hash_file ($ algo , $ filename )) {
77+ throw new HashException ('could not hash file ' );
78+ }
79+
80+ $ storedHashesFile = $ this ->getParameter (self ::STORED_HASH_FILE );
81+
82+
83+ $ file = fopen ($ storedHashesFile , 'a+ ' );
84+ if (!$ file ) {
85+ throw new FileHandlerException ('file not found ' );
86+ }
87+ $ this ->checkHeaderExists ($ file );
88+
89+
90+ try {
91+ $ filenameExists = $ this ->csvFileHandler ->searchInCsvFile (
92+ filename: $ storedHashesFile ,
93+ keyword: $ filename ,
94+ column: 'File '
95+ );
96+
97+ if (!$ filenameExists ) {
98+ fputcsv ($ file , [$ filename , $ hash ]);
99+ }
100+ } catch (FileHandlerException ) {
101+ fputcsv ($ file , [$ filename , $ hash ]);
102+ } finally {
103+ fclose ($ file );
104+ }
105+
106+
107+ return $ hash ;
108+ }
109+
110+ /**
111+ * @param mixed $storedHashFile
112+ * @return void
113+ */
114+ private function checkHeaderExists (mixed $ storedHashFile ): void
115+ {
116+ $ header = fgetcsv ($ storedHashFile );
117+
118+ if (!$ header || $ header [0 ] !== self ::SEARCH_COLUMN_NAME || $ header [1 ] !== self ::SEARCH_COLUMN_VALUE ) {
119+ fseek ($ storedHashFile , 0 );
120+ fputcsv ($ storedHashFile , [self ::SEARCH_COLUMN_NAME , self ::SEARCH_COLUMN_VALUE ]);
121+ fflush ($ storedHashFile );
122+ }
72123 }
73124}
0 commit comments