summaryrefslogtreecommitdiff
path: root/hash/ed2k.cpp
blob: 92e7b15fba7b8a5f5cbabe9bf060db1fb68be46b (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
#include "ed2k.h"

template<class T>
inline T min(T a, T b) {
	return (a > b) ? b : a;
}

Ed2k::Ed2k() {
	MD4_Init(&md4_partial);
	MD4_Init(&md4_final);
	size_total = 0;
}

void Ed2k::update(const char* data, int length) {
	while(length) {
		if(!(size_total % (9500 * 1024)) && size_total) {
			unsigned char digest[16];
			MD4_Final(digest, &md4_partial);
			MD4_Update(&md4_final, digest, 16);
			MD4_Init(&md4_partial);
		}
		int size = min<int>(length, (9500 * 1024) - (size_total % (9500 * 1024)));
		MD4_Update(&md4_partial, data, size);
		length -= size;
		data += size;
		size_total += size;
	};
}

char* Ed2k::digest() {
	char* digest = new char[16];
	if(size_total > (9500 * 1024)) {
		unsigned char digest_partial[16];
		MD4_Final(digest_partial, &md4_partial);
		MD4_Update(&md4_final, digest_partial, 16);
		MD4_Final((unsigned char*)digest, &md4_final);
	} else {
		MD4_Final((unsigned char*)digest, &md4_partial);
	}
	return digest;
}