1
1
/*
2
2
-------------------------------------------------------------------------------
3
3
This file is part of the SHA-256 hash calculator.
4
- Copyright (C) 2012 Thoronador
4
+ Copyright (C) 2012, 2015 Thoronador
5
5
6
6
This program is free software: you can redistribute it and/or modify
7
7
it under the terms of the GNU General Public License as published by
22
22
#include < set>
23
23
#include < string>
24
24
#include " ../libthoro/filesystem/FileFunctions.hpp"
25
+ #include " ../libthoro/hash/sha512/sha512.hpp"
26
+ #include " ../libthoro/hash/sha512/FileSource.hpp"
27
+ #include " ../libthoro/hash/sha512/FileSourceUtility.hpp"
25
28
#include " ../libthoro/hash/sha256/sha256.hpp"
26
29
#include " ../libthoro/hash/sha256/FileSource.hpp"
27
30
#include " ../libthoro/hash/sha256/FileSourceUtility.hpp"
@@ -40,49 +43,50 @@ void showGPLNotice()
40
43
std::cout << " SHA-256 file hash calculator\n "
41
44
<< " Copyright (C) 2012, 2015 Thoronador\n "
42
45
<< " \n "
43
- << " This programme is free software: you can redistribute it and/or\n "
46
+ << " This program is free software: you can redistribute it and/or\n "
44
47
<< " modify it under the terms of the GNU General Public License as published\n "
45
48
<< " by the Free Software Foundation, either version 3 of the License, or\n "
46
49
<< " (at your option) any later version.\n "
47
50
<< " \n "
48
- << " This programme is distributed in the hope that they will be useful,\n "
51
+ << " This program is distributed in the hope that they will be useful,\n "
49
52
<< " but WITHOUT ANY WARRANTY; without even the implied warranty of\n "
50
53
<< " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n "
51
54
<< " GNU General Public License for more details.\n "
52
55
<< " \n "
53
56
<< " You should have received a copy of the GNU General Public License\n "
54
- << " along with this programme . If not, see <http://www.gnu.org/licenses/>.\n "
57
+ << " along with this program . If not, see <http://www.gnu.org/licenses/>.\n "
55
58
<< " \n " ;
56
59
}
57
60
58
61
void showVersion ()
59
62
{
60
63
showGPLNotice ();
61
- std::cout << " SHA-256 file hash calculator, version 1.2 , 2015-08-01 \n " ;
64
+ std::cout << " SHA-256 file hash calculator, version 1.3 , 2015-08-03 \n " ;
62
65
}
63
66
64
67
void showHelp ()
65
68
{
66
- std::cout << " \n sha256 [--sha1 | --sha224 | --sha256] FILENAME\n "
69
+ std::cout << " \n sha256 [--sha1 | --sha224 | --sha256 | --sha512 ] FILENAME\n "
67
70
<< " \n "
68
71
<< " options:\n "
69
72
<< " --help - displays this help message and quits\n "
70
73
<< " -? - same as --help\n "
71
- << " --version - displays the version of the programme and quits\n "
74
+ << " --version - displays the version of the program and quits\n "
72
75
<< " -v - same as --version\n "
73
76
<< " FILENAME - set path to file that should be hashed. Can be repeated\n "
74
- << " multiple times.\n "
77
+ << " multiple times. Has to appear at least once. \n "
75
78
<< " --sha1 - use SHA-1 instead of SHA-256 to hash files.\n "
76
79
<< " --sha224 - use SHA-224 instead of SHA-256 to hash files.\n "
77
80
<< " --sha256 - use SHA-256 to hash files. This option is active by\n "
78
- << " default.\n " ;
81
+ << " default.\n "
82
+ << " --sha512 - use SHA-512 instead of SHA-256 to hash files.\n " ;
79
83
}
80
84
81
85
int main (int argc, char **argv)
82
86
{
83
87
std::set<std::string> files;
84
88
85
- enum SHAHashType {htUnspecified, htSHA1, htSHA224, htSHA256 };
89
+ enum SHAHashType {htUnspecified, htSHA1, htSHA224, htSHA256, htSHA512 };
86
90
87
91
SHAHashType hashType = htUnspecified;
88
92
@@ -152,6 +156,21 @@ int main(int argc, char **argv)
152
156
}
153
157
hashType = htSHA256;
154
158
}// sha-256
159
+ else if ((param==" --sha512" ) or (param==" --sha-512" ))
160
+ {
161
+ if (hashType==htSHA512)
162
+ {
163
+ std::cout << " Error: parameter " << param << " must not occur more than once!\n " ;
164
+ return rcInvalidParameter;
165
+ }
166
+ if (hashType!=htUnspecified)
167
+ {
168
+ std::cout << " Error: parameter " << param << " must not occur "
169
+ << " after hash type has already been set!\n " ;
170
+ return rcInvalidParameter;
171
+ }
172
+ hashType = htSHA512;
173
+ }// sha-512
155
174
else
156
175
{
157
176
// should be filename
@@ -179,17 +198,26 @@ int main(int argc, char **argv)
179
198
}// if arguments present
180
199
else
181
200
{
182
- std::cout << " You have to specify certain parameters for this programme to run properly.\n "
201
+ std::cout << " You have to specify certain parameters for this program to run properly.\n "
202
+ << " Use --help to get a list of valid parameters.\n " ;
203
+ return rcInvalidParameter;
204
+ }
205
+
206
+ if (files.empty ())
207
+ {
208
+ std::cout << " You have to specify certain parameters for this program to run properly.\n "
183
209
<< " Use --help to get a list of valid parameters.\n " ;
184
210
return rcInvalidParameter;
185
211
}
186
212
213
+ // Set default hash algorithm, if no choice was made.
187
214
if (hashType==htUnspecified)
188
215
hashType = htSHA256;
189
216
190
- std::cout << std::endl << " Hashing file(s), this may take a while..." << std::endl;
217
+ std::cout << " Hashing file(s), this may take a while..." << std::endl;
191
218
192
219
std::set<std::string>::const_iterator iter = files.begin ();
220
+ SHA512::MessageDigest hash512;
193
221
SHA256::MessageDigest hash256;
194
222
SHA224::MessageDigest hash224;
195
223
SHA1::MessageDigest hash160;
@@ -205,6 +233,10 @@ int main(int argc, char **argv)
205
233
hash224 = SHA224::computeFromFile (*iter);
206
234
std::cout << hash224.toHexString () << " " << *iter << std::endl;
207
235
break ;
236
+ case htSHA512:
237
+ hash512 = SHA512::computeFromFile (*iter);
238
+ std::cout << hash512.toHexString () << " " << *iter << std::endl;
239
+ break ;
208
240
default :
209
241
hash256 = SHA256::computeFromFile (*iter);
210
242
std::cout << hash256.toHexString () << " " << *iter << std::endl;
0 commit comments