apt @VERSION@

md5.h

00001 // -*- mode: cpp; mode: fold -*-
00002 // Description                                                          /*{{{*/
00003 // $Id: md5.h,v 1.6 2001/05/07 05:06:52 jgg Exp $
00004 /* ######################################################################
00005    
00006    MD5SumValue - Storage for a MD5Sum
00007    MD5Summation - MD5 Message Digest Algorithm.
00008    
00009    This is a C++ interface to a set of MD5Sum functions. The class can
00010    store a MD5Sum in 16 bytes of memory.
00011    
00012    A MD5Sum is used to generate a (hopefully) unique 16 byte number for a
00013    block of data. This can be used to gaurd against corruption of a file.
00014    MD5 should not be used for tamper protection, use SHA or something more
00015    secure.
00016    
00017    There are two classes because computing a MD5 is not a continual 
00018    operation unless 64 byte blocks are used. Also the summation requires an
00019    extra 18*4 bytes to operate.
00020    
00021    ##################################################################### */
00022                                                                         /*}}}*/
00023 #ifndef APTPKG_MD5_H
00024 #define APTPKG_MD5_H
00025 
00026 
00027 #include <string>
00028 #include <cstring>
00029 #include <algorithm>
00030 #include <stdint.h>
00031 
00032 using std::string;
00033 using std::min;
00034 
00035 class MD5Summation;
00036 
00037 class MD5SumValue
00038 {
00039    friend class MD5Summation;
00040    unsigned char Sum[4*4];
00041    
00042    public:
00043 
00044    // Accessors
00045    bool operator ==(const MD5SumValue &rhs) const; 
00046    string Value() const;
00047    inline void Value(unsigned char S[16]) 
00048          {for (int I = 0; I != sizeof(Sum); I++) S[I] = Sum[I];};
00049    inline operator string() const {return Value();};
00050    bool Set(string Str);
00051    inline void Set(unsigned char S[16]) 
00052          {for (int I = 0; I != sizeof(Sum); I++) Sum[I] = S[I];};
00053 
00054    MD5SumValue(string Str);
00055    MD5SumValue();
00056 };
00057 
00058 class MD5Summation
00059 {
00060    uint32_t Buf[4];
00061    unsigned char Bytes[2*4];
00062    unsigned char In[16*4];
00063    bool Done;
00064    
00065    public:
00066 
00067    bool Add(const unsigned char *Data,unsigned long Size);
00068    inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));};
00069    bool AddFD(int Fd,unsigned long Size);
00070    inline bool Add(const unsigned char *Beg,const unsigned char *End) 
00071                   {return Add(Beg,End-Beg);};
00072    MD5SumValue Result();
00073    
00074    MD5Summation();
00075 };
00076 
00077 #endif