|
a/src/utils/md5.cpp |
|
b/src/utils/md5.cpp |
|
... |
|
... |
25 |
* $FreeBSD: src/lib/libmd/md5c.c,v 1.11 1999/12/29 05:04:20 peter Exp $
|
25 |
* $FreeBSD: src/lib/libmd/md5c.c,v 1.11 1999/12/29 05:04:20 peter Exp $
|
26 |
*
|
26 |
*
|
27 |
* This code is the same as the code published by RSA Inc. It has been
|
27 |
* This code is the same as the code published by RSA Inc. It has been
|
28 |
* edited for clarity and style only.
|
28 |
* edited for clarity and style only.
|
29 |
*/
|
29 |
*/
|
30 |
|
30 |
#ifndef TEST_MD5
|
31 |
#include <string.h>
|
31 |
#include <string.h>
|
32 |
|
32 |
|
33 |
#include "md5.h"
|
33 |
#include "md5.h"
|
34 |
|
34 |
|
35 |
typedef unsigned int md5uint32;
|
35 |
typedef unsigned int md5uint32;
|
|
... |
|
... |
314 |
state[3] += d;
|
314 |
state[3] += d;
|
315 |
|
315 |
|
316 |
/* Zeroize sensitive information. */
|
316 |
/* Zeroize sensitive information. */
|
317 |
memset ((void *)x, 0, sizeof (x));
|
317 |
memset ((void *)x, 0, sizeof (x));
|
318 |
}
|
318 |
}
|
|
|
319 |
|
|
|
320 |
/*************** Convenience / utilities */
|
|
|
321 |
void MD5Final(string &digest, MD5_CTX *context)
|
|
|
322 |
{
|
|
|
323 |
unsigned char d[16];
|
|
|
324 |
MD5Final (d, context);
|
|
|
325 |
digest.assign((const char *)d, 16);
|
|
|
326 |
}
|
|
|
327 |
|
|
|
328 |
string& MD5String(const string& data, string& digest)
|
|
|
329 |
{
|
|
|
330 |
MD5_CTX ctx;
|
|
|
331 |
MD5Init(&ctx);
|
|
|
332 |
MD5Update(&ctx, (const unsigned char*)data.c_str(), data.length());
|
|
|
333 |
MD5Final(digest, &ctx);
|
|
|
334 |
return digest;
|
|
|
335 |
}
|
|
|
336 |
|
|
|
337 |
string& MD5HexPrint(const string& digest, string &out)
|
|
|
338 |
{
|
|
|
339 |
out.erase();
|
|
|
340 |
out.reserve(33);
|
|
|
341 |
static const char hex[]="0123456789abcdef";
|
|
|
342 |
const unsigned char *hash = (const unsigned char *)digest.c_str();
|
|
|
343 |
for (int i = 0; i < 16; i++) {
|
|
|
344 |
out.append(1, hex[hash[i] >> 4]);
|
|
|
345 |
out.append(1, hex[hash[i] & 0x0f]);
|
|
|
346 |
}
|
|
|
347 |
return out;
|
|
|
348 |
}
|
|
|
349 |
string& MD5HexScan(const string& xdigest, string& digest)
|
|
|
350 |
{
|
|
|
351 |
digest.erase();
|
|
|
352 |
if (xdigest.length() != 32) {
|
|
|
353 |
return digest;
|
|
|
354 |
}
|
|
|
355 |
for (unsigned int i = 0; i < 16; i++) {
|
|
|
356 |
unsigned int val;
|
|
|
357 |
if (sscanf(xdigest.c_str() + 2*i, "%2x", &val) != 1) {
|
|
|
358 |
digest.erase();
|
|
|
359 |
return digest;
|
|
|
360 |
}
|
|
|
361 |
digest.append(1, (unsigned char)val);
|
|
|
362 |
}
|
|
|
363 |
return digest;
|
|
|
364 |
}
|
|
|
365 |
|
|
|
366 |
#include "readfile.h"
|
|
|
367 |
class FileScanMd5 : public FileScanDo {
|
|
|
368 |
public:
|
|
|
369 |
FileScanMd5(string& d) : digest(d) {}
|
|
|
370 |
virtual bool init(unsigned int size, string *reason)
|
|
|
371 |
{
|
|
|
372 |
MD5Init(&ctx);
|
|
|
373 |
return true;
|
|
|
374 |
}
|
|
|
375 |
virtual bool data(const char *buf, int cnt, string* reason)
|
|
|
376 |
{
|
|
|
377 |
MD5Update(&ctx, (const unsigned char*)buf, cnt);
|
|
|
378 |
return true;
|
|
|
379 |
}
|
|
|
380 |
string &digest;
|
|
|
381 |
MD5_CTX ctx;
|
|
|
382 |
};
|
|
|
383 |
bool MD5File(const string& filename, string &digest, string *reason)
|
|
|
384 |
{
|
|
|
385 |
FileScanMd5 md5er(digest);
|
|
|
386 |
if (!file_scan(filename, &md5er, reason))
|
|
|
387 |
return false;
|
|
|
388 |
// We happen to know that digest and md5er.digest are the same object
|
|
|
389 |
MD5Final(md5er.digest, &md5er.ctx);
|
|
|
390 |
return true;
|
|
|
391 |
}
|
|
|
392 |
#else
|
|
|
393 |
|
|
|
394 |
// Test driver
|
|
|
395 |
#include <stdlib.h>
|
|
|
396 |
|
|
|
397 |
#include <string>
|
|
|
398 |
#include <iostream>
|
|
|
399 |
#include "md5.h"
|
|
|
400 |
|
|
|
401 |
using namespace std;
|
|
|
402 |
|
|
|
403 |
static const char *thisprog;
|
|
|
404 |
static char usage [] =
|
|
|
405 |
"trmd5 filename\n\n"
|
|
|
406 |
;
|
|
|
407 |
static void
|
|
|
408 |
Usage(void)
|
|
|
409 |
{
|
|
|
410 |
fprintf(stderr, "%s: usage:\n%s", thisprog, usage);
|
|
|
411 |
exit(1);
|
|
|
412 |
}
|
|
|
413 |
|
|
|
414 |
int main(int argc, const char **argv)
|
|
|
415 |
{
|
|
|
416 |
thisprog = argv[0];
|
|
|
417 |
argc--; argv++;
|
|
|
418 |
|
|
|
419 |
if (argc != 1)
|
|
|
420 |
Usage();
|
|
|
421 |
string filename = *argv++;argc--;
|
|
|
422 |
|
|
|
423 |
string reason, digest;
|
|
|
424 |
if (!MD5File(filename, digest, &reason)) {
|
|
|
425 |
cerr << reason << endl;
|
|
|
426 |
exit(1);
|
|
|
427 |
} else {
|
|
|
428 |
string hex;
|
|
|
429 |
cout << "MD5 (" << filename << ") = " << MD5HexPrint(digest, hex) << endl;
|
|
|
430 |
|
|
|
431 |
string digest1;
|
|
|
432 |
MD5HexScan(hex, digest1);
|
|
|
433 |
if (digest1.compare(digest)) {
|
|
|
434 |
cout << "MD5HexScan Failure" << endl;
|
|
|
435 |
cout << MD5HexPrint(digest, hex) << " " << digest.length() << " -> "
|
|
|
436 |
<< MD5HexPrint(digest1, hex) << " " << digest1.length() << endl;
|
|
|
437 |
exit(1);
|
|
|
438 |
}
|
|
|
439 |
|
|
|
440 |
}
|
|
|
441 |
exit(0);
|
|
|
442 |
}
|
|
|
443 |
|
|
|
444 |
#endif
|