"Fossies" - the Fresh Open Source Software Archive 
Member "netxms-3.8.166/src/tools/nxcsum/nxcsum.cpp" (23 Feb 2021, 3686 Bytes) of package /linux/misc/netxms-3.8.166.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "nxcsum.cpp" see the
Fossies "Dox" file reference documentation and the last
Fossies "Diffs" side-by-side code changes report:
3.6.300_vs_3.7.95.
1 /*
2 ** nxcsum - command line tool for checksum calculation
3 ** Copyright (C) 2018-2020 Raden Solutions
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 **
19 ** File: nxcsum.cpp
20 **
21 **/
22
23 #include <nms_util.h>
24 #include <netxms-version.h>
25
26 NETXMS_EXECUTABLE_HEADER(nxcsum)
27
28 #define MAX_READ_SIZE 4096
29
30 union HashState
31 {
32 MD5_STATE md5;
33 SHA1_STATE sha1;
34 SHA224_STATE sha224;
35 SHA256_STATE sha256;
36 SHA384_STATE sha384;
37 SHA512_STATE sha512;
38 };
39
40 static bool PrintHash(const char *fileName, size_t digestSize, void (*init)(HashState*), void (*update)(HashState*, BYTE*, size_t), void (*finish)(HashState*, BYTE*))
41 {
42 bool useStdin = (strcmp(fileName, "-") == 0);
43
44 #ifdef _WIN32
45 int hFile;
46 if (useStdin)
47 {
48 hFile = _fileno(stdin);
49 _setmode(hFile, _O_BINARY);
50 }
51 else
52 {
53 hFile = _sopen(fileName, O_RDONLY | O_BINARY, SH_DENYWR);
54 }
55 #else
56 int hFile = useStdin ? fileno(stdin) : _open(fileName, O_RDONLY | O_BINARY);
57 #endif
58 if (hFile == -1)
59 {
60 _ftprintf(stderr, _T("Cannot open input file (%hs)\n"), strerror(errno));
61 return false;
62 }
63
64 BYTE buffer[MAX_READ_SIZE];
65
66 HashState state;
67 init(&state);
68
69 while(true)
70 {
71 int readSize = _read(hFile, buffer, MAX_READ_SIZE);
72 if (readSize <= 0)
73 break;
74 update(&state, buffer, readSize);
75 }
76
77 if (!useStdin)
78 _close(hFile);
79
80 BYTE *hash = (BYTE *)MemAlloc(digestSize);
81 finish(&state, hash);
82
83 TCHAR *dump = MemAllocArray<TCHAR>(digestSize * 2 + 1);
84 _tprintf(_T("%s"), _tcslwr(BinToStr(hash, digestSize, dump)));
85 MemFree(dump);
86 MemFree(hash);
87 return true;
88 }
89
90 #define HashInit(t) t##Init
91 #define HashUpdate(t) t##Update
92 #define HashFinal(t) t##Final
93 #define HashSize(t) t##_DIGEST_SIZE
94
95 #define CallPrintHash(f, t) \
96 PrintHash(f, HashSize(t), (void (*)(HashState*))HashInit(t), (void (*)(HashState*, BYTE*, size_t))HashUpdate(t), (void (*)(HashState*, BYTE*))HashFinal(t));
97
98 /**
99 * main
100 */
101 int main(int argc, char *argv[])
102 {
103 InitNetXMSProcess(true);
104
105 if((argc < 2))
106 {
107 printf("Invalid count of arguments\n"
108 "Usage: nxcsum <method> <file>\n"
109 "Valid methods are: md5, sha1, sha224, sha256, sha384, sha512\n"
110 "\n");
111 return 1;
112 }
113
114 bool success;
115 if (!strcmp(argv[1], "md5"))
116 {
117 success = CallPrintHash(argv[2], MD5);
118 }
119 else if(!strcmp(argv[1], "sha1"))
120 {
121 success = CallPrintHash(argv[2], SHA1);
122 }
123 else if(!strcmp(argv[1], "sha224"))
124 {
125 success = CallPrintHash(argv[2], SHA224);
126 }
127 else if(!strcmp(argv[1], "sha256"))
128 {
129 success = CallPrintHash(argv[2], SHA256);
130 }
131 else if(!strcmp(argv[1], "sha384"))
132 {
133 success = CallPrintHash(argv[2], SHA384);
134 }
135 else if(!strcmp(argv[1], "sha512"))
136 {
137 success = CallPrintHash(argv[2], SHA512);
138 }
139 else
140 {
141 printf("Invalid method\n"
142 "Valid methods are: md5, sha1, sha224, sha256, sha384, sha512\n"
143 "\n");
144 return 1;
145 }
146
147 return success ? 0 : 2;
148 }