Last change
on this file since 50 was
2,
checked in by hhi, 13 years ago
|
inital import
|
-
Property svn:eol-style set to
native
|
File size:
962 bytes
|
Line | |
---|
1 | |
---|
2 | #pragma once |
---|
3 | #include "libmd5.h" |
---|
4 | |
---|
5 | class MD5 { |
---|
6 | public: |
---|
7 | /** |
---|
8 | * initialize digest state |
---|
9 | */ |
---|
10 | MD5() |
---|
11 | { |
---|
12 | MD5Init(&m_state); |
---|
13 | } |
---|
14 | |
---|
15 | /** |
---|
16 | * compute digest over @buf of length @len. |
---|
17 | * multiple calls may extend the digest over more data. |
---|
18 | */ |
---|
19 | void update(unsigned char *buf, unsigned len) |
---|
20 | { |
---|
21 | MD5Update(&m_state, buf, len); |
---|
22 | } |
---|
23 | |
---|
24 | /** |
---|
25 | * flush any outstanding MD5 data, write the digest into @digest. |
---|
26 | */ |
---|
27 | void finalize(unsigned char digest[16]) |
---|
28 | { |
---|
29 | MD5Final(digest, &m_state); |
---|
30 | } |
---|
31 | |
---|
32 | private: |
---|
33 | context_md5_t m_state; |
---|
34 | }; |
---|
35 | |
---|
36 | /** |
---|
37 | * Produce an ascii(hex) representation of the 128bit @digest. |
---|
38 | * |
---|
39 | * Returns: a statically allocated null-terminated string. DO NOT FREE. |
---|
40 | */ |
---|
41 | inline const char* |
---|
42 | digestToString(unsigned char digest[16]) |
---|
43 | { |
---|
44 | const char* hex = "0123456789abcdef"; |
---|
45 | static char string[33]; |
---|
46 | for (int i = 0; i < 16; i++) |
---|
47 | { |
---|
48 | string[i*2+0] = hex[digest[i] >> 4]; |
---|
49 | string[i*2+1] = hex[digest[i] & 0xf]; |
---|
50 | } |
---|
51 | return string; |
---|
52 | } |
---|
53 | |
---|
Note: See
TracBrowser for help on using the repository browser.