-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMD5Wrapper.cc
85 lines (70 loc) · 1.5 KB
/
MD5Wrapper.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// -*- c++ -*-
// Copyright 2009 Isis Innovation Limited
/*
* A wrapper class for md5 to provide some simple hashing fucntions.
* Some code originally by Benjamin Grüdelbach (Feb 2005)
*
* Author: Bob Castle
*/
#include <fstream>
#include <iostream>
#include "MD5Wrapper.h"
#include "MD5.h"
namespace PTAMM {
/**
* Constructor
*/
MD5Wrapper::MD5Wrapper()
: mMD5( new MD5() )
{
}
/**
* Destructor
*/
MD5Wrapper::~MD5Wrapper()
{
delete mMD5;
}
/**
* converts the numeric hash to
* a valid std::string.
* (based on Jim Howard's code;
* http://www.codeproject.com/cpp/cmd5.asp)
* @param bytes char array
* @return string
*/
std::string MD5Wrapper::convToString(unsigned char *bytes)
{
char asciihash[33];
int p = 0;
for(int i=0; i<16; i++)
{
::sprintf(&asciihash[p],"%02x",bytes[i]);
p += 2;
}
asciihash[32] = '\0';
return std::string(asciihash);
}
/**
* Calculate the hash from a block of data
* @param byte the data block
* @param nBytesToRead the number of bytes to read
* @param sMD5Hash the returned hash
* @return success
*/
bool MD5Wrapper::getHashFromData(const unsigned char *byte, unsigned int nBytesToRead, std::string & sMD5Hash)
{
if( byte == NULL ) {
return "-1";
}
//init md5
MD5_CTX context;
mMD5->MD5Init( &context );
mMD5->MD5Update( &context, byte, nBytesToRead );
// generate hash and return the hash as std::string
unsigned char digest[16];
mMD5->MD5Final( digest, &context );
sMD5Hash = convToString(digest);
return true;
}
}