@@ -64,26 +64,26 @@ void showGPLNotice()
64
64
void showVersion ()
65
65
{
66
66
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 " ;
68
68
}
69
69
70
70
void showHelp ()
71
71
{
72
72
std::cout << " \n sha256 [--sha1 | --sha224 | --sha256 | --sha384 | --sha512] FILENAME\n "
73
73
<< " \n "
74
74
<< " options:\n "
75
- << " --help - displays this help message and quits\n "
75
+ << " --help - Displays this help message and quits. \n "
76
76
<< " -? - 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 "
78
78
<< " -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 "
80
80
<< " 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 "
84
84
<< " 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 " ;
87
87
}
88
88
89
89
int main (int argc, char **argv)
@@ -94,180 +94,178 @@ int main(int argc, char **argv)
94
94
95
95
SHAHashType hashType = htUnspecified;
96
96
97
- if ((argc> 1 ) and (argv!= NULL ))
97
+ if ((argc > 1 ) && (argv != nullptr ))
98
98
{
99
- int i= 1 ;
100
- while (i< argc)
99
+ int i = 1 ;
100
+ while (i < argc)
101
101
{
102
- if (argv[i]!= NULL )
102
+ if (argv[i] != nullptr )
103
103
{
104
104
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 == " /?" ))
107
107
{
108
108
showHelp ();
109
109
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" ))
113
113
{
114
114
showVersion ();
115
115
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" ))
119
119
{
120
- if (hashType== htSHA1)
120
+ if (hashType == htSHA1)
121
121
{
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 " ;
123
123
return rcInvalidParameter;
124
124
}
125
- if (hashType!= htUnspecified)
125
+ if (hashType != htUnspecified)
126
126
{
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 " ;
129
129
return rcInvalidParameter;
130
130
}
131
131
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" ))
134
134
{
135
- if (hashType== htSHA224)
135
+ if (hashType == htSHA224)
136
136
{
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 " ;
138
138
return rcInvalidParameter;
139
139
}
140
- if (hashType!= htUnspecified)
140
+ if (hashType != htUnspecified)
141
141
{
142
- std::cout << " Error: parameter " << param << " must not occur "
142
+ std::cerr << " Error: Parameter " << param << " must not occur "
143
143
<< " after hash type has already been set!\n " ;
144
144
return rcInvalidParameter;
145
145
}
146
146
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" ))
149
149
{
150
- if (hashType== htSHA256)
150
+ if (hashType == htSHA256)
151
151
{
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 " ;
153
153
return rcInvalidParameter;
154
154
}
155
- if (hashType!= htUnspecified)
155
+ if (hashType != htUnspecified)
156
156
{
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 " ;
159
159
return rcInvalidParameter;
160
160
}
161
161
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" ))
164
164
{
165
- if (hashType== htSHA384)
165
+ if (hashType == htSHA384)
166
166
{
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 " ;
168
168
return rcInvalidParameter;
169
169
}
170
- if (hashType!= htUnspecified)
170
+ if (hashType != htUnspecified)
171
171
{
172
- std::cout << " Error: parameter " << param << " must not occur "
172
+ std::cerr << " Error: Parameter " << param << " must not occur "
173
173
<< " after hash type has already been set!\n " ;
174
174
return rcInvalidParameter;
175
175
}
176
176
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" ))
179
179
{
180
- if (hashType== htSHA512)
180
+ if (hashType == htSHA512)
181
181
{
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 " ;
183
183
return rcInvalidParameter;
184
184
}
185
- if (hashType!= htUnspecified)
185
+ if (hashType != htUnspecified)
186
186
{
187
- std::cout << " Error: parameter " << param << " must not occur "
187
+ std::cerr << " Error: Parameter " << param << " must not occur "
188
188
<< " after hash type has already been set!\n " ;
189
189
return rcInvalidParameter;
190
190
}
191
191
hashType = htSHA512;
192
- }// sha-512
192
+ } // sha-512
193
193
else
194
194
{
195
- // should be filename
195
+ // should be filename
196
196
if (libstriezel::filesystem::file::exists (param))
197
197
{
198
- // add file to list
198
+ // add file to list
199
199
files.insert (param);
200
200
}
201
201
else
202
202
{
203
- std::cout << " Invalid parameter/filename given: \" " << param
203
+ std::cerr << " Invalid parameter/filename given: \" " << param
204
204
<< " \" does not name an existing file!\n "
205
205
<< " Use --help to get a list of valid parameters.\n " ;
206
206
return rcInvalidParameter;
207
207
}
208
208
}
209
- }// parameter exists
209
+ } // parameter exists
210
210
else
211
211
{
212
- std::cout << " Parameter at index " <<i<< " is NULL.\n " ;
212
+ std::cerr << " Parameter at index " << i << " is NULL.\n " ;
213
213
return rcInvalidParameter;
214
214
}
215
- ++i;// on to next parameter
216
- }// while
217
- }// if arguments present
215
+ ++i; // on to next parameter
216
+ }
217
+ }
218
218
else
219
219
{
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 "
221
221
<< " Use --help to get a list of valid parameters.\n " ;
222
222
return rcInvalidParameter;
223
223
}
224
224
225
225
if (files.empty ())
226
226
{
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 "
228
228
<< " Use --help to get a list of valid parameters.\n " ;
229
229
return rcInvalidParameter;
230
230
}
231
231
232
232
// Set default hash algorithm, if no choice was made.
233
- if (hashType== htUnspecified)
233
+ if (hashType == htUnspecified)
234
234
hashType = htSHA256;
235
235
236
236
std::cout << " Hashing file(s), this may take a while..." << std::endl;
237
237
238
- std::set<std::string>::const_iterator iter = files.begin ();
239
238
SHA512::MessageDigest hash512;
240
239
SHA384::MessageDigest hash384;
241
240
SHA256::MessageDigest hash256;
242
241
SHA224::MessageDigest hash224;
243
242
SHA1::MessageDigest hash160;
244
- while (iter!= files. end () )
243
+ for ( const auto & item: files)
245
244
{
246
245
switch (hashType)
247
246
{
248
247
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;
251
250
break ;
252
251
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;
255
254
break ;
256
255
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;
259
258
break ;
260
259
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;
263
262
break ;
264
263
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;
267
266
break ;
268
- }// swi
269
- ++iter;
270
- }// while
267
+ }
268
+ }
271
269
272
270
return 0 ;
273
271
}
0 commit comments