apt  0.9.14.1+nmu1
hashes.h
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: hashes.h,v 1.2 2001/03/11 05:30:20 jgg Exp $
4 /* ######################################################################
5 
6  Hashes - Simple wrapper around the hash functions
7 
8  This is just used to make building the methods simpler, this is the
9  only interface required..
10 
11  ##################################################################### */
12  /*}}}*/
13 #ifndef APTPKG_HASHES_H
14 #define APTPKG_HASHES_H
15 
16 
17 #include <apt-pkg/md5.h>
18 #include <apt-pkg/sha1.h>
19 #include <apt-pkg/sha2.h>
20 #include <apt-pkg/fileutl.h>
21 
22 #include <algorithm>
23 #include <vector>
24 #include <cstring>
25 
26 
27 #ifndef APT_8_CLEANER_HEADERS
28 using std::min;
29 using std::vector;
30 #endif
31 
32 // helper class that contains hash function name
33 // and hash
35 {
36  protected:
37  std::string Type;
38  std::string Hash;
39  static const char* _SupportedHashes[10];
40 
41  // internal helper
42  std::string GetHashForFile(std::string filename) const;
43 
44  public:
45  HashString(std::string Type, std::string Hash);
46  HashString(std::string StringedHashString); // init from str as "type:hash"
47  HashString();
48 
49  // get hash type used
50  std::string HashType() { return Type; };
51 
52  // verify the given filename against the currently loaded hash
53  bool VerifyFile(std::string filename) const;
54 
55  // generate a hash string from the given filename
56  bool FromFile(std::string filename);
57 
58 
59  // helper
60  std::string toStr() const; // convert to str as "type:hash"
61  bool empty() const;
62 
63  // return the list of hashes we support
64  static const char** SupportedHashes();
65 };
66 
67 class Hashes
68 {
69  public:
70 
71  MD5Summation MD5;
72  SHA1Summation SHA1;
73  SHA256Summation SHA256;
74  SHA512Summation SHA512;
75 
76  inline bool Add(const unsigned char *Data,unsigned long long Size)
77  {
78  return MD5.Add(Data,Size) && SHA1.Add(Data,Size) && SHA256.Add(Data,Size) && SHA512.Add(Data,Size);
79  };
80  inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));};
81  inline bool AddFD(int const Fd,unsigned long long Size = 0)
82  { return AddFD(Fd, Size, true, true, true, true); };
83  bool AddFD(int const Fd, unsigned long long Size, bool const addMD5,
84  bool const addSHA1, bool const addSHA256, bool const addSHA512);
85  inline bool AddFD(FileFd &Fd,unsigned long long Size = 0)
86  { return AddFD(Fd, Size, true, true, true, true); };
87  bool AddFD(FileFd &Fd, unsigned long long Size, bool const addMD5,
88  bool const addSHA1, bool const addSHA256, bool const addSHA512);
89  inline bool Add(const unsigned char *Beg,const unsigned char *End)
90  {return Add(Beg,End-Beg);};
91 };
92 
93 #endif
Definition: fileutl.h:41
Definition: hashes.h:34
Definition: hashes.h:67
Definition: sha2.h:38
Definition: sha2.h:71
Definition: sha1.h:30
Definition: md5.h:41