1 /*
2  *  Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All rights reserved.
3  *
4  *  License to copy and use this software is granted provided that it is identified
5  *  as the "RSA Data Security, Inc. MD5 Message-Digest Algorithm" in all material
6  *  mentioning or referencing this software or this function.
7  *
8  *  License is also granted to make and use derivative works provided that such
9  *  works are identified as "derived from the RSA Data Security, Inc. MD5 Message-
10  *  Digest Algorithm" in all material mentioning or referencing the derived work.
11  *
12  *  RSA Data Security, Inc. makes no representations concerning either the
13  *  merchantability of this software or the suitability of this software for any
14  *  particular purpose. It is provided "as is" without express or implied warranty
15  *  of any kind. These notices must be retained in any copies of any part of this
16  *  documentation and/or software.
17  */
18 
19 #ifndef MD5SUM_MD5_H
20 #define MD5SUM_MD5_H
21 
22 namespace APE
23 {
24 
25 typedef unsigned int uint32_t;
26 typedef unsigned char uint8_t;
27 
28 /*
29  *  Define the MD5 context structure
30  *  Please DO NOT change the order or contents of the structure as various assembler files depend on it !!
31  */
32 
33 typedef struct {
34    uint32_t  state  [ 4];     /* state (ABCD) */
35    uint32_t  count  [ 2];     /* number of bits, modulo 2^64 (least sig word first) */
36    uint8_t   buffer [64];     /* input buffer for incomplete buffer data */
37 } MD5_CTX;
38 
39 void   MD5Init   ( MD5_CTX* ctx );
40 void   MD5Update ( MD5_CTX* ctx, const uint8_t* buf, size_t len );
41 void   MD5Final  ( uint8_t digest [16], MD5_CTX* ctx );
42 
43 class CMD5Helper
44 {
45 public:
46     CMD5Helper(bool bInitialize = true)
47     {
48         if (bInitialize)
49             Initialize();
50     }
51 
Initialize()52     bool Initialize()
53     {
54         memset(&m_MD5Context, 0, sizeof(m_MD5Context));
55         MD5Init(&m_MD5Context);
56         m_nTotalBytes = 0;
57         return true;
58     }
59 
AddData(const void * pData,int nBytes)60     __forceinline void AddData(const void * pData, int nBytes)
61     {
62         MD5Update(&m_MD5Context, (const unsigned char *) pData, nBytes);
63         m_nTotalBytes += nBytes;
64     }
65 
GetResult(unsigned char cResult[16])66     bool GetResult(unsigned char cResult[16])
67     {
68         memset(cResult, 0, 16);
69         MD5Final(cResult, &m_MD5Context);
70         return true;
71     }
72 
73 protected:
74     MD5_CTX m_MD5Context;
75     bool m_bStopped;
76     int m_nTotalBytes;
77 };
78 
79 }
80 
81 #endif /* MD5SUM_MD5_H */
82