summaryrefslogtreecommitdiff
path: root/lib/crypto/MD5Digest.cpp
blob: 58cc90eea7e4bb27b61372dd49cde51cb7668d91 (plain)
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
// --------------------------------------------------------------------------
//
// File
//		Name:    MD5Digest.cpp
//		Purpose: Simple interface for creating MD5 digests
//		Created: 8/12/03
//
// --------------------------------------------------------------------------


#include "Box.h"

#include "MD5Digest.h"

#include "MemLeakFindOn.h"


MD5Digest::MD5Digest()
{
	MD5_Init(&md5);
	for(unsigned int l = 0; l < sizeof(mDigest); ++l)
	{
		mDigest[l] = 0;
	}
}

MD5Digest::~MD5Digest()
{
}

void MD5Digest::Add(const std::string &rString)
{
	MD5_Update(&md5, rString.c_str(), rString.size());
}

void MD5Digest::Add(const void *pData, int Length)
{
	MD5_Update(&md5, pData, Length);
}

void MD5Digest::Finish()
{
	MD5_Final(mDigest, &md5);
}

std::string MD5Digest::DigestAsString()
{
	std::string r;

	static const char *hex = "0123456789abcdef";

	for(unsigned int l = 0; l < sizeof(mDigest); ++l)
	{
		r += hex[(mDigest[l] & 0xf0) >> 4];
		r += hex[(mDigest[l] & 0x0f)];
	}

	return r;
}

int MD5Digest::CopyDigestTo(uint8_t *to)
{
	for(int l = 0; l < MD5_DIGEST_LENGTH; ++l)
	{
		to[l] = mDigest[l];
	}

	return MD5_DIGEST_LENGTH;
}


bool MD5Digest::DigestMatches(uint8_t *pCompareWith) const
{
	for(int l = 0; l < MD5_DIGEST_LENGTH; ++l)
	{
		if(pCompareWith[l] != mDigest[l])
			return false;
	}

	return true;
}