@@ -64,26 +64,26 @@ void showGPLNotice()
6464void showVersion ()
6565{
6666 showGPLNotice ();
67- std::cout << " SHA-256 file hash calculator, version 1.4, 2015-08-06 \n " ;
67+ std::cout << " SHA-256 file hash calculator, version 1.5, 2022-11-12 \n " ;
6868}
6969
7070void showHelp ()
7171{
7272 std::cout << " \n sha256 [--sha1 | --sha224 | --sha256 | --sha384 | --sha512] FILENAME\n "
7373 << " \n "
7474 << " options:\n "
75- << " --help - displays this help message and quits\n "
75+ << " --help - Displays this help message and quits. \n "
7676 << " -? - same as --help\n "
77- << " --version - displays the version of the program and quits\n "
77+ << " --version - Displays the version of the program and quits. \n "
7878 << " -v - same as --version\n "
79- << " FILENAME - set path to file that should be hashed. Can be repeated\n "
79+ << " FILENAME - Set path to file that should be hashed. Can be repeated\n "
8080 << " multiple times. Has to appear at least once.\n "
81- << " --sha1 - use SHA-1 instead of SHA-256 to hash files.\n "
82- << " --sha224 - use SHA-224 instead of SHA-256 to hash files.\n "
83- << " --sha256 - use SHA-256 to hash files. This option is active by\n "
81+ << " --sha1 - Use SHA-1 instead of SHA-256 to hash files.\n "
82+ << " --sha224 - Use SHA-224 instead of SHA-256 to hash files.\n "
83+ << " --sha256 - Use SHA-256 to hash files. This option is active by\n "
8484 << " default.\n "
85- << " --sha384 - use SHA-384 instead of SHA-256 to hash files.\n "
86- << " --sha512 - use SHA-512 instead of SHA-256 to hash files.\n " ;
85+ << " --sha384 - Use SHA-384 instead of SHA-256 to hash files.\n "
86+ << " --sha512 - Use SHA-512 instead of SHA-256 to hash files.\n " ;
8787}
8888
8989int main (int argc, char **argv)
@@ -94,180 +94,178 @@ int main(int argc, char **argv)
9494
9595 SHAHashType hashType = htUnspecified;
9696
97- if ((argc> 1 ) and (argv!= NULL ))
97+ if ((argc > 1 ) && (argv != nullptr ))
9898 {
99- int i= 1 ;
100- while (i< argc)
99+ int i = 1 ;
100+ while (i < argc)
101101 {
102- if (argv[i]!= NULL )
102+ if (argv[i] != nullptr )
103103 {
104104 const std::string param = std::string (argv[i]);
105- // help parameter
106- if ((param== " --help" ) or (param== " -?" ) or (param== " /?" ))
105+ // help parameter
106+ if ((param == " --help" ) || (param == " -?" ) || (param == " /?" ))
107107 {
108108 showHelp ();
109109 return 0 ;
110- }// if help wanted
111- // version information requested?
112- else if ((param== " --version" ) or (param== " -v" ))
110+ }
111+ // version information requested?
112+ else if ((param == " --version" ) || (param == " -v" ))
113113 {
114114 showVersion ();
115115 return 0 ;
116- }// version
117- else if ((param== " --sha1" ) or (param== " --sha-1" ) or (param== " --sha160" )
118- or (param== " --sha-160" ))
116+ }
117+ else if ((param == " --sha1" ) || (param == " --sha-1" ) || (param == " --sha160" )
118+ || (param == " --sha-160" ))
119119 {
120- if (hashType== htSHA1)
120+ if (hashType == htSHA1)
121121 {
122- std::cout << " Error: parameter " << param<< " must not occur more than once!\n " ;
122+ std::cerr << " Error: Parameter " << param << " must not occur more than once!\n " ;
123123 return rcInvalidParameter;
124124 }
125- if (hashType!= htUnspecified)
125+ if (hashType != htUnspecified)
126126 {
127- std::cout << " Error: parameter " << param<< " must not occur after "
128- << " hash type has already been set!\n " ;
127+ std::cerr << " Error: Parameter " << param << " must not occur "
128+ << " after hash type has already been set!\n " ;
129129 return rcInvalidParameter;
130130 }
131131 hashType = htSHA1;
132- }// sha-1
133- else if ((param== " --sha224" ) or (param== " --sha-224" ))
132+ } // sha-1
133+ else if ((param == " --sha224" ) || (param == " --sha-224" ))
134134 {
135- if (hashType== htSHA224)
135+ if (hashType == htSHA224)
136136 {
137- std::cout << " Error: parameter " << param << " must not occur more than once!\n " ;
137+ std::cerr << " Error: Parameter " << param << " must not occur more than once!\n " ;
138138 return rcInvalidParameter;
139139 }
140- if (hashType!= htUnspecified)
140+ if (hashType != htUnspecified)
141141 {
142- std::cout << " Error: parameter " << param << " must not occur "
142+ std::cerr << " Error: Parameter " << param << " must not occur "
143143 << " after hash type has already been set!\n " ;
144144 return rcInvalidParameter;
145145 }
146146 hashType = htSHA224;
147- }// sha-224
148- else if ((param== " --sha256" ) or (param== " --sha-256" ))
147+ } // sha-224
148+ else if ((param == " --sha256" ) || (param == " --sha-256" ))
149149 {
150- if (hashType== htSHA256)
150+ if (hashType == htSHA256)
151151 {
152- std::cout << " Error: parameter " << param<< " must not occur more than once!\n " ;
152+ std::cerr << " Error: Parameter " << param << " must not occur more than once!\n " ;
153153 return rcInvalidParameter;
154154 }
155- if (hashType!= htUnspecified)
155+ if (hashType != htUnspecified)
156156 {
157- std::cout << " Error: parameter " << param<< " must not occur after "
158- << " hash type has already been set!\n " ;
157+ std::cerr << " Error: Parameter " << param << " must not occur "
158+ << " after hash type has already been set!\n " ;
159159 return rcInvalidParameter;
160160 }
161161 hashType = htSHA256;
162- }// sha-256
163- else if ((param== " --sha384" ) or (param== " --sha-384" ))
162+ } // sha-256
163+ else if ((param == " --sha384" ) || (param == " --sha-384" ))
164164 {
165- if (hashType== htSHA384)
165+ if (hashType == htSHA384)
166166 {
167- std::cout << " Error: parameter " << param << " must not occur more than once!\n " ;
167+ std::cerr << " Error: Parameter " << param << " must not occur more than once!\n " ;
168168 return rcInvalidParameter;
169169 }
170- if (hashType!= htUnspecified)
170+ if (hashType != htUnspecified)
171171 {
172- std::cout << " Error: parameter " << param << " must not occur "
172+ std::cerr << " Error: Parameter " << param << " must not occur "
173173 << " after hash type has already been set!\n " ;
174174 return rcInvalidParameter;
175175 }
176176 hashType = htSHA384;
177- }// sha-384
178- else if ((param== " --sha512" ) or (param== " --sha-512" ))
177+ } // sha-384
178+ else if ((param == " --sha512" ) || (param == " --sha-512" ))
179179 {
180- if (hashType== htSHA512)
180+ if (hashType == htSHA512)
181181 {
182- std::cout << " Error: parameter " << param << " must not occur more than once!\n " ;
182+ std::cerr << " Error: Parameter " << param << " must not occur more than once!\n " ;
183183 return rcInvalidParameter;
184184 }
185- if (hashType!= htUnspecified)
185+ if (hashType != htUnspecified)
186186 {
187- std::cout << " Error: parameter " << param << " must not occur "
187+ std::cerr << " Error: Parameter " << param << " must not occur "
188188 << " after hash type has already been set!\n " ;
189189 return rcInvalidParameter;
190190 }
191191 hashType = htSHA512;
192- }// sha-512
192+ } // sha-512
193193 else
194194 {
195- // should be filename
195+ // should be filename
196196 if (libstriezel::filesystem::file::exists (param))
197197 {
198- // add file to list
198+ // add file to list
199199 files.insert (param);
200200 }
201201 else
202202 {
203- std::cout << " Invalid parameter/filename given: \" " << param
203+ std::cerr << " Invalid parameter/filename given: \" " << param
204204 << " \" does not name an existing file!\n "
205205 << " Use --help to get a list of valid parameters.\n " ;
206206 return rcInvalidParameter;
207207 }
208208 }
209- }// parameter exists
209+ } // parameter exists
210210 else
211211 {
212- std::cout << " Parameter at index " <<i<< " is NULL.\n " ;
212+ std::cerr << " Parameter at index " << i << " is NULL.\n " ;
213213 return rcInvalidParameter;
214214 }
215- ++i;// on to next parameter
216- }// while
217- }// if arguments present
215+ ++i; // on to next parameter
216+ }
217+ }
218218 else
219219 {
220- std::cout << " You have to specify certain parameters for this program to run properly.\n "
220+ std::cerr << " You have to specify certain parameters for this program to run properly.\n "
221221 << " Use --help to get a list of valid parameters.\n " ;
222222 return rcInvalidParameter;
223223 }
224224
225225 if (files.empty ())
226226 {
227- std::cout << " You have to specify certain parameters for this program to run properly.\n "
227+ std::cerr << " You have to specify certain parameters for this program to run properly.\n "
228228 << " Use --help to get a list of valid parameters.\n " ;
229229 return rcInvalidParameter;
230230 }
231231
232232 // Set default hash algorithm, if no choice was made.
233- if (hashType== htUnspecified)
233+ if (hashType == htUnspecified)
234234 hashType = htSHA256;
235235
236236 std::cout << " Hashing file(s), this may take a while..." << std::endl;
237237
238- std::set<std::string>::const_iterator iter = files.begin ();
239238 SHA512::MessageDigest hash512;
240239 SHA384::MessageDigest hash384;
241240 SHA256::MessageDigest hash256;
242241 SHA224::MessageDigest hash224;
243242 SHA1::MessageDigest hash160;
244- while (iter!= files. end () )
243+ for ( const auto & item: files)
245244 {
246245 switch (hashType)
247246 {
248247 case htSHA1:
249- hash160 = SHA1::computeFromFile (*iter );
250- std::cout << hash160.toHexString () << " " << *iter << std::endl;
248+ hash160 = SHA1::computeFromFile (item );
249+ std::cout << hash160.toHexString () << " " << item << std::endl;
251250 break ;
252251 case htSHA224:
253- hash224 = SHA224::computeFromFile (*iter );
254- std::cout << hash224.toHexString () << " " << *iter << std::endl;
252+ hash224 = SHA224::computeFromFile (item );
253+ std::cout << hash224.toHexString () << " " << item << std::endl;
255254 break ;
256255 case htSHA384:
257- hash384 = SHA384::computeFromFile (*iter );
258- std::cout << hash384.toHexString () << " " << *iter << std::endl;
256+ hash384 = SHA384::computeFromFile (item );
257+ std::cout << hash384.toHexString () << " " << item << std::endl;
259258 break ;
260259 case htSHA512:
261- hash512 = SHA512::computeFromFile (*iter );
262- std::cout << hash512.toHexString () << " " << *iter << std::endl;
260+ hash512 = SHA512::computeFromFile (item );
261+ std::cout << hash512.toHexString () << " " << item << std::endl;
263262 break ;
264263 default :
265- hash256 = SHA256::computeFromFile (*iter );
266- std::cout << hash256.toHexString () << " " << *iter << std::endl;
264+ hash256 = SHA256::computeFromFile (item );
265+ std::cout << hash256.toHexString () << " " << item << std::endl;
267266 break ;
268- }// swi
269- ++iter;
270- }// while
267+ }
268+ }
271269
272270 return 0 ;
273271}
0 commit comments