forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
md5wrapper.cpp
161 lines (141 loc) · 3.18 KB
/
md5wrapper.cpp
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* This is part of my wrapper-class to create
* a MD5 Hash from a string and a file.
*
* This code is completly free, you
* can copy it, modify it, or do
* what ever you want with it.
*
* Feb. 2005
* Benjamin Grüdelbach
*/
/*
* Changed unsigned long int types into uint32_t to make this work on 64bit systems.
* Sep. 5. 2009
* Petr Mrázek
*/
//----------------------------------------------------------------------
//basic includes
#include <algorithm>
#include <fstream>
#include <iostream>
#include <errno.h>
#include <string.h>
//my includes
#include "md5wrapper.h"
#include "md5.h"
//---------privates--------------------------
/*
* internal hash function, calling
* the basic methods from md5.h
*/
std::string md5wrapper::hashit(unsigned char *data, size_t length)
{
MD5Context ctx;
//init md5
MD5Init(&ctx);
//update with our string
MD5Update(&ctx, data, length);
//create the hash
unsigned char buff[16] = "";
MD5Final((unsigned char*)buff,&ctx);
//converte the hash to a string and return it
return convToString(buff);
}
/*
* converts the numeric hash to
* a valid std::string.
* (based on Jim Howard's code;
* http://www.codeproject.com/cpp/cmd5.asp)
*/
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);
}
//---------publics--------------------------
//constructor
md5wrapper::md5wrapper()
{
}
//destructor
md5wrapper::~md5wrapper()
{
}
/*
* creates a MD5 hash from
* "text" and returns it as
* string
*/
std::string md5wrapper::getHashFromString(std::string text)
{
return this->hashit((unsigned char*)text.data(), text.length());
}
/*
* creates a MD5 hash from
* a file specified in "filename" and
* returns it as string
* (based on Ronald L. Rivest's code
* from RFC1321 "The MD5 Message-Digest Algorithm")
*/
std::string md5wrapper::getHashFromFile(std::string filename, uint32_t & length, char * first_kb)
{
FILE *file;
MD5Context context;
int len;
int saved = 0;
unsigned char buffer[1024], digest[16];
//open file
if ((file = fopen (filename.c_str(), "rb")) == NULL)
{
return "file unreadable.";
}
length = 0;
//init md5
MD5Init (&context);
//read the filecontent
while (1)
{
errno = 0;
len = fread (buffer, 1, 1024, file);
if(saved < 1024 && first_kb)
{
memcpy(first_kb + saved, buffer, std::min (len, 1024 - saved));
saved += len;
}
length += len;
if(len != 1024)
{
int err = ferror(file);
//FIXME: check errno here.
if(err)
{
fclose(file);
return strerror(err);
}
if(feof(file))
{
MD5Update (&context, buffer, len);
break;
}
}
MD5Update (&context, buffer, len);
}
/*
generate hash, close the file and return the
hash as std::string
*/
MD5Final (digest, &context);
fclose (file);
return convToString(digest);
}
/*
* EOF
*/