apt  @VERSION@
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/sha2.h>
00020 #include <apt-pkg/fileutl.h>
00021 
00022 #include <algorithm>
00023 #include <vector>
00024 #include <cstring>
00025 
00026 
00027 #ifndef APT_8_CLEANER_HEADERS
00028 using std::min;
00029 using std::vector;
00030 #endif
00031 
00032 // helper class that contains hash function name
00033 // and hash
00034 class HashString
00035 {
00036  protected:
00037    std::string Type;
00038    std::string Hash;
00039    static const char * _SupportedHashes[10];
00040 
00041  public:
00042    HashString(std::string Type, std::string Hash);
00043    HashString(std::string StringedHashString);  // init from str as "type:hash"
00044    HashString();
00045 
00046    // get hash type used
00047    std::string HashType() { return Type; };
00048 
00049    // verify the given filename against the currently loaded hash
00050    bool VerifyFile(std::string filename) const;
00051 
00052    // helper
00053    std::string toStr() const;                    // convert to str as "type:hash"
00054    bool empty() const;
00055 
00056    // return the list of hashes we support
00057    static const char** SupportedHashes();
00058 };
00059 
00060 class Hashes
00061 {
00062    public:
00063 
00064    MD5Summation MD5;
00065    SHA1Summation SHA1;
00066    SHA256Summation SHA256;
00067    SHA512Summation SHA512;
00068    
00069    inline bool Add(const unsigned char *Data,unsigned long long Size)
00070    {
00071       return MD5.Add(Data,Size) && SHA1.Add(Data,Size) && SHA256.Add(Data,Size) && SHA512.Add(Data,Size);
00072    };
00073    inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));};
00074    inline bool AddFD(int const Fd,unsigned long long Size = 0)
00075    { return AddFD(Fd, Size, true, true, true, true); };
00076    bool AddFD(int const Fd, unsigned long long Size, bool const addMD5,
00077               bool const addSHA1, bool const addSHA256, bool const addSHA512);
00078    inline bool AddFD(FileFd &Fd,unsigned long long Size = 0)
00079    { return AddFD(Fd, Size, true, true, true, true); };
00080    bool AddFD(FileFd &Fd, unsigned long long Size, bool const addMD5,
00081               bool const addSHA1, bool const addSHA256, bool const addSHA512);
00082    inline bool Add(const unsigned char *Beg,const unsigned char *End) 
00083                   {return Add(Beg,End-Beg);};
00084 };
00085 
00086 #endif