]> git.decadent.org.uk Git - dak.git/blob - tools/dsync-0.0/libdsync/contrib/md5.h
Merge upstream
[dak.git] / tools / dsync-0.0 / libdsync / contrib / md5.h
1 // -*- mode: cpp; mode: fold -*-
2 // Description                                                          /*{{{*/
3 // $Id: md5.h,v 1.4 1999/10/24 06:53:12 jgg Exp $
4 /* ######################################################################
5    
6    MD5SumValue - Storage for a MD5Sum
7    MD5Summation - MD5 Message Digest Algorithm.
8    
9    This is a C++ interface to a set of MD5Sum functions. The class can
10    store a MD5Sum in 16 bytes of memory.
11    
12    A MD5Sum is used to generate a (hopefully) unique 16 byte number for a
13    block of data. This can be used to gaurd against corruption of a file.
14    MD5 should not be used for tamper protection, use SHA or something more
15    secure.
16    
17    There are two classes because computing a MD5 is not a continual 
18    operation unless 64 byte blocks are used. Also the summation requires an
19    extra 18*4 bytes to operate.
20    
21    ##################################################################### */
22                                                                         /*}}}*/
23 #ifndef APTPKG_MD5_H
24 #define APTPKG_MD5_H
25
26 #ifdef __GNUG__
27 #pragma interface "dsync/md5.h"
28 #endif 
29
30 #include <string>
31 using namespace std;
32
33 class MD5Summation;
34
35 class MD5SumValue
36 {
37    friend class MD5Summation;
38    unsigned char Sum[4*4];
39    
40    public:
41
42    // Accessors
43    bool operator ==(const MD5SumValue &rhs) const; 
44    string Value() const;
45    inline void Value(unsigned char S[16]) 
46          {for (int I = 0; I != sizeof(Sum); I++) S[I] = Sum[I];};
47    inline operator string() const {return Value();};
48    bool Set(string Str);
49    inline void Set(unsigned char S[16]) 
50          {for (int I = 0; I != sizeof(Sum); I++) Sum[I] = S[I];};
51
52    MD5SumValue(string Str);
53    MD5SumValue();
54 };
55
56 class MD5Summation
57 {
58    unsigned char Buf[4*4];
59    unsigned char Bytes[2*4];
60    unsigned char In[16*4];
61    bool Done;
62    
63    public:
64
65    bool Add(const unsigned char *Data,unsigned long Size);
66    inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));};
67    bool AddFD(int Fd,unsigned long Size);
68    inline bool Add(const unsigned char *Beg,const unsigned char *End) 
69                   {return Add(Beg,End-Beg);};
70    MD5SumValue Result();
71    
72    MD5Summation();
73 };
74
75 #endif