hashes.h

00001 // -*- mode: cpp; mode: fold -*-
00002 // Description                                                          /*{{{*/
00003 // $Id: hashes.h,v 1.2 2001/03/11 05:30:20 jgg Exp $
00004 /* ######################################################################
00005 
00006    Hashes - Simple wrapper around the hash functions
00007    
00008    This is just used to make building the methods simpler, this is the
00009    only interface required..
00010    
00011    ##################################################################### */
00012                                                                         /*}}}*/
00013 #ifndef APTPKG_HASHES_H
00014 #define APTPKG_HASHES_H
00015 
00016 
00017 #include <apt-pkg/md5.h>
00018 #include <apt-pkg/sha1.h>
00019 #include <apt-pkg/sha256.h>
00020 
00021 #include <algorithm>
00022 #include <vector>
00023 #include <cstring>
00024 
00025 using std::min;
00026 using std::vector;
00027 
00028 class FileFd;
00029 
00030 // helper class that contains hash function name
00031 // and hash
00032 class HashString
00033 {
00034  protected:
00035    string Type;
00036    string Hash;
00037    static const char * _SupportedHashes[10];
00038 
00039  public:
00040    HashString(string Type, string Hash);
00041    HashString(string StringedHashString);  // init from str as "type:hash"
00042    HashString();
00043 
00044    // get hash type used
00045    string HashType() { return Type; };
00046 
00047    // verify the given filename against the currently loaded hash
00048    bool VerifyFile(string filename) const;
00049 
00050    // helper
00051    string toStr() const;                    // convert to str as "type:hash"
00052    bool empty() const;
00053 
00054    // return the list of hashes we support
00055    static const char** SupportedHashes();
00056 };
00057 
00058 class Hashes
00059 {
00060    public:
00061 
00062    MD5Summation MD5;
00063    SHA1Summation SHA1;
00064    SHA256Summation SHA256;
00065    
00066    inline bool Add(const unsigned char *Data,unsigned long Size)
00067    {
00068       return MD5.Add(Data,Size) && SHA1.Add(Data,Size) && SHA256.Add(Data,Size);
00069    };
00070    inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));};
00071    bool AddFD(int Fd,unsigned long Size);
00072    inline bool AddFD(FileFd &Fd,unsigned long Size = 0)
00073    { return AddFD(Fd, Size, true, true, true, true); };
00074    bool AddFD(FileFd &Fd, unsigned long Size, bool const addMD5,
00075               bool const addSHA1, bool const addSHA256, bool const addSHA512);
00076 
00077    inline bool Add(const unsigned char *Beg,const unsigned char *End) 
00078                   {return Add(Beg,End-Beg);};
00079 };
00080 
00081 #endif