xref: /haiku/src/add-ons/media/plugins/ape_reader/MAClib/MD5.h (revision 820dca4df6c7bf955c46e8f6521b9408f50b2900)
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 typedef unsigned int uint32_t;
23 typedef unsigned char uint8_t;
24 
25 /*
26  *  Define the MD5 context structure
27  *  Please DO NOT change the order or contents of the structure as various assembler files depend on it !!
28  */
29 
30 typedef struct {
31    uint32_t  state  [ 4];     /* state (ABCD) */
32    uint32_t  count  [ 2];     /* number of bits, modulo 2^64 (least sig word first) */
33    uint8_t   buffer [64];     /* input buffer for incomplete buffer data */
34 } MD5_CTX;
35 
36 void   MD5Init   ( MD5_CTX* ctx );
37 void   MD5Update ( MD5_CTX* ctx, const uint8_t* buf, size_t len );
38 void   MD5Final  ( uint8_t digest [16], MD5_CTX* ctx );
39 
40 class CMD5Helper
41 {
42 public:
43 
44     CMD5Helper(BOOL bInitialize = TRUE)
45     {
46         if (bInitialize)
47             Initialize();
48     }
49 
50     BOOL Initialize()
51     {
52         memset(&m_MD5Context, 0, sizeof(m_MD5Context));
53         MD5Init(&m_MD5Context);
54         m_nTotalBytes = 0;
55         return TRUE;
56     }
57 
58     inline void AddData(const void * pData, int nBytes)
59     {
60         MD5Update(&m_MD5Context, (const unsigned char *) pData, nBytes);
61         m_nTotalBytes += nBytes;
62     }
63 
64     BOOL GetResult(unsigned char cResult[16])
65     {
66         memset(cResult, 0, 16);
67         MD5Final(cResult, &m_MD5Context);
68         return TRUE;
69     }
70 
71 protected:
72 
73     MD5_CTX m_MD5Context;
74     BOOL m_bStopped;
75     int m_nTotalBytes;
76 };
77 
78 
79 #endif /* MD5SUM_MD5_H */
80