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