VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTDigest.cpp@ 29250

Last change on this file since 29250 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.0 KB
Line 
1/* $Id: tstRTDigest.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * IPRT Testcase - RTSha*, RTMd5, RTCrc*.
4 */
5
6/*
7 * Copyright (C) 2009 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include <iprt/sha.h>
32#include <iprt/md5.h>
33#include <iprt/crc32.h>
34#include <iprt/crc64.h>
35
36#include <iprt/ctype.h>
37#include <iprt/err.h>
38#include <iprt/file.h>
39#include <iprt/getopt.h>
40#include <iprt/initterm.h>
41#include <iprt/param.h>
42#include <iprt/path.h>
43#include <iprt/process.h>
44#include <iprt/string.h>
45#include <iprt/stream.h>
46
47
48static int Error(const char *pszFormat, ...)
49{
50 char szName[RTPATH_MAX];
51 if (!RTProcGetExecutableName(szName, sizeof(szName)))
52 strcpy(szName, "tstRTDigest");
53
54 RTStrmPrintf(g_pStdErr, "%s: error: ", RTPathFilename(szName));
55 va_list va;
56 va_start(va, pszFormat);
57 RTStrmPrintfV(g_pStdErr, pszFormat, va);
58 va_end(va);
59
60 return 1;
61}
62
63
64int main(int argc, char **argv)
65{
66 RTR3Init();
67
68 enum
69 {
70 kDigestType_NotSpecified,
71 kDigestType_CRC32,
72 kDigestType_CRC64,
73 kDigestType_MD5,
74 kDigestType_SHA1,
75 kDigestType_SHA256,
76 kDigestType_SHA512
77 } enmDigestType = kDigestType_NotSpecified;
78
79 enum
80 {
81 kMethod_Full,
82 kMethod_Block,
83 kMethod_File
84 } enmMethod = kMethod_Block;
85
86 static const RTGETOPTDEF s_aOptions[] =
87 {
88 { "--type", 't', RTGETOPT_REQ_STRING },
89 { "--method", 'm', RTGETOPT_REQ_STRING },
90 { "--help", 'h', RTGETOPT_REQ_NOTHING },
91 };
92
93 int ch;
94 RTGETOPTUNION ValueUnion;
95 RTGETOPTSTATE GetState;
96 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0);
97 while ((ch = RTGetOpt(&GetState, &ValueUnion)))
98 {
99 switch (ch)
100 {
101 case 't':
102 if (!RTStrICmp(ValueUnion.psz, "crc32"))
103 enmDigestType = kDigestType_CRC32;
104 else if (!RTStrICmp(ValueUnion.psz, "crc64"))
105 enmDigestType = kDigestType_CRC64;
106 else if (!RTStrICmp(ValueUnion.psz, "md5"))
107 enmDigestType = kDigestType_MD5;
108 else if (!RTStrICmp(ValueUnion.psz, "sha1"))
109 enmDigestType = kDigestType_SHA1;
110 else if (!RTStrICmp(ValueUnion.psz, "sha256"))
111 enmDigestType = kDigestType_SHA256;
112 else if (!RTStrICmp(ValueUnion.psz, "sha512"))
113 enmDigestType = kDigestType_SHA512;
114 else
115 {
116 Error("Invalid digest type: %s\n", ValueUnion.psz);
117 return 1;
118 }
119 break;
120
121 case 'm':
122 if (!RTStrICmp(ValueUnion.psz, "full"))
123 enmMethod = kMethod_Full;
124 else if (!RTStrICmp(ValueUnion.psz, "block"))
125 enmMethod = kMethod_Block;
126 else if (!RTStrICmp(ValueUnion.psz, "file"))
127 enmMethod = kMethod_File;
128 else
129 {
130 Error("Invalid digest method: %s\n", ValueUnion.psz);
131 return 1;
132 }
133 break;
134
135 case 'h':
136 RTPrintf("syntax: tstRTDigest -t <digest-type> file [file2 [..]]\n");
137 return 1;
138
139 case VINF_GETOPT_NOT_OPTION:
140 {
141 if (enmDigestType == kDigestType_NotSpecified)
142 return Error("No digest type was specified\n");
143
144 switch (enmMethod)
145 {
146 case kMethod_Full:
147 return Error("Full file method is not implemented\n");
148
149 case kMethod_File:
150 switch (enmDigestType)
151 {
152 case kDigestType_SHA1:
153 {
154 char *pszDigest;
155 int rc = RTSha1Digest(ValueUnion.psz, &pszDigest);
156 if (RT_FAILURE(rc))
157 return Error("RTSha1Digest(%s,) -> %Rrc\n", ValueUnion.psz, rc);
158 RTPrintf("%s %s\n", pszDigest, ValueUnion.psz);
159 RTStrFree(pszDigest);
160 break;
161 }
162
163 default:
164 return Error("The file method isn't implemented for this digest\n");
165 }
166 break;
167
168 case kMethod_Block:
169 {
170 RTFILE hFile;
171 int rc = RTFileOpen(&hFile, ValueUnion.psz, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
172 if (RT_FAILURE(rc))
173 return Error("RTFileOpen(,%s,) -> %Rrc\n", ValueUnion.psz, rc);
174
175 size_t cbRead;
176 uint8_t abBuf[_64K];
177 char *pszDigest = (char *)&abBuf[0];
178 switch (enmDigestType)
179 {
180 case kDigestType_CRC32:
181 {
182 uint32_t uCRC32 = RTCrc32Start();
183 for (;;)
184 {
185 rc = RTFileRead(hFile, abBuf, sizeof(abBuf), &cbRead);
186 if (RT_FAILURE(rc) || !cbRead)
187 break;
188 uCRC32 = RTCrc32Process(uCRC32, abBuf, cbRead);
189 }
190 uCRC32 = RTCrc32Finish(uCRC32);
191 RTStrPrintf(pszDigest, sizeof(abBuf), "%08RX32", uCRC32);
192 break;
193 }
194
195 case kDigestType_CRC64:
196 {
197 uint64_t uCRC64 = RTCrc64Start();
198 for (;;)
199 {
200 rc = RTFileRead(hFile, abBuf, sizeof(abBuf), &cbRead);
201 if (RT_FAILURE(rc) || !cbRead)
202 break;
203 uCRC64 = RTCrc64Process(uCRC64, abBuf, cbRead);
204 }
205 uCRC64 = RTCrc64Finish(uCRC64);
206 RTStrPrintf(pszDigest, sizeof(abBuf), "%016RX64", uCRC64);
207 break;
208 }
209
210 case kDigestType_MD5:
211 {
212 RTMD5CONTEXT Ctx;
213 RTMd5Init(&Ctx);
214 for (;;)
215 {
216 rc = RTFileRead(hFile, abBuf, sizeof(abBuf), &cbRead);
217 if (RT_FAILURE(rc) || !cbRead)
218 break;
219 RTMd5Update(&Ctx, abBuf, cbRead);
220 }
221 uint8_t abDigest[RTMD5HASHSIZE];
222 RTMd5Final(abDigest, &Ctx);
223 RTMd5ToString(abDigest, pszDigest, sizeof(abBuf));
224 break;
225 }
226
227 case kDigestType_SHA1:
228 {
229 RTSHA1CONTEXT Ctx;
230 RTSha1Init(&Ctx);
231 for (;;)
232 {
233 rc = RTFileRead(hFile, abBuf, sizeof(abBuf), &cbRead);
234 if (RT_FAILURE(rc) || !cbRead)
235 break;
236 RTSha1Update(&Ctx, abBuf, cbRead);
237 }
238 uint8_t abDigest[RTSHA1_HASH_SIZE];
239 RTSha1Final(&Ctx, abDigest);
240 RTSha1ToString(abDigest, pszDigest, sizeof(abBuf));
241 break;
242 }
243
244 case kDigestType_SHA256:
245 {
246 RTSHA256CONTEXT Ctx;
247 RTSha256Init(&Ctx);
248 for (;;)
249 {
250 rc = RTFileRead(hFile, abBuf, sizeof(abBuf), &cbRead);
251 if (RT_FAILURE(rc) || !cbRead)
252 break;
253 RTSha256Update(&Ctx, abBuf, cbRead);
254 }
255 uint8_t abDigest[RTSHA256_HASH_SIZE];
256 RTSha256Final(&Ctx, abDigest);
257 RTSha256ToString(abDigest, pszDigest, sizeof(abBuf));
258 break;
259 }
260
261 case kDigestType_SHA512:
262 {
263 RTSHA512CONTEXT Ctx;
264 RTSha512Init(&Ctx);
265 for (;;)
266 {
267 rc = RTFileRead(hFile, abBuf, sizeof(abBuf), &cbRead);
268 if (RT_FAILURE(rc) || !cbRead)
269 break;
270 RTSha512Update(&Ctx, abBuf, cbRead);
271 }
272 uint8_t abDigest[RTSHA512_HASH_SIZE];
273 RTSha512Final(&Ctx, abDigest);
274 RTSha512ToString(abDigest, pszDigest, sizeof(abBuf));
275 break;
276 }
277
278 default:
279 return Error("Internal error #1\n");
280 }
281 RTFileClose(hFile);
282 if (RT_FAILURE(rc) && rc != VERR_EOF)
283 {
284 RTPrintf("Partial: %s %s\n", pszDigest, ValueUnion.psz);
285 return Error("RTFileRead(%s) -> %Rrc\n", ValueUnion.psz, rc);
286 }
287 RTPrintf("%s %s\n", pszDigest, ValueUnion.psz);
288 break;
289 }
290
291 default:
292 return Error("Internal error #2\n");
293 }
294 break;
295 }
296
297 default:
298 return RTGetOptPrintError(ch, &ValueUnion);
299 }
300 }
301
302 return 0;
303}
304
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette