-
Notifications
You must be signed in to change notification settings - Fork 0
/
Encoder.cc
62 lines (48 loc) · 1.36 KB
/
Encoder.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
#include <iostream>
#include <string.h>
#include "Encoder.h"
static void calculateMD5(const char * text, char * checksum){
static const char hexDigits[17] = "0123456789ABCDEF";
unsigned char digest[MD5_DIGEST_LENGTH];
char digest_str[2*MD5_DIGEST_LENGTH];
int i;
// Count digest
MD5((const unsigned char*)text, sizeof(text), digest);
// Convert the hash into a hex string form
for(i = 0; i < MD5_DIGEST_LENGTH; i++){
digest_str[i*2] = hexDigits[(digest[i] >> 4) & 0xF];
digest_str[i*2+1] = hexDigits[digest[i] & 0xF];
}
digest_str[MD5_DIGEST_LENGTH*2] = '\0';
sprintf(checksum, digest_str, "%s");
}
Encoder::Encoder(){
m = new message;
sprintf(m->text, "%s", "\0");
sprintf(m->checksum, "%s", "\0");
m->status = 0;
}
Encoder::~Encoder(){
m = NULL;
}
void Encoder::encodeMessage(const message &msg) {
sprintf(m->text, msg.text, "%s");
calculateMD5(m->text, m->checksum);
m->status = msg.status;
}
bool Encoder::decodeMessage(const message &msg){
char newChecksum[2*MD5_DIGEST_LENGTH];
sprintf(m->text, msg.text, "%s");
sprintf(m->checksum, msg.checksum, "%s");
m->status = msg.status;
calculateMD5(m->text, newChecksum);
if(!strncmp(m->checksum, newChecksum, 2*MD5_DIGEST_LENGTH))
return true;
return false;
}
message * Encoder::getMessage(){
return m;
}
char * Encoder::deliveryMessage(){
return m->text;
}