]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/hashes.h
use 'best' hash for source authentication
[apt.git] / apt-pkg / contrib / 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/macros.h>
21
22 #include <cstring>
23 #include <string>
24
25 #ifndef APT_8_CLEANER_HEADERS
26 using std::min;
27 using std::vector;
28 #endif
29 #ifndef APT_10_CLEANER_HEADERS
30 #include <apt-pkg/fileutl.h>
31 #include <algorithm>
32 #include <vector>
33 #endif
34
35
36 class FileFd;
37
38 // helper class that contains hash function name
39 // and hash
40 class HashString
41 {
42 protected:
43 std::string Type;
44 std::string Hash;
45 static const char * _SupportedHashes[10];
46
47 // internal helper
48 std::string GetHashForFile(std::string filename) const;
49
50 public:
51 HashString(std::string Type, std::string Hash);
52 HashString(std::string StringedHashString); // init from str as "type:hash"
53 HashString();
54
55 // get hash type used
56 std::string HashType() const { return Type; };
57 std::string HashValue() const { return Hash; };
58
59 // verify the given filename against the currently loaded hash
60 bool VerifyFile(std::string filename) const;
61
62 // generate a hash string from the given filename
63 bool FromFile(std::string filename);
64
65
66 // helper
67 std::string toStr() const; // convert to str as "type:hash"
68 bool empty() const;
69 bool operator==(HashString const &other) const;
70 bool operator!=(HashString const &other) const;
71
72 // return the list of hashes we support
73 static APT_CONST const char** SupportedHashes();
74 };
75
76 class HashStringList
77 {
78 public:
79 /** find best hash if no specific one is requested
80 *
81 * @param type of the checksum to return, can be \b NULL
82 * @return If type is \b NULL (or the empty string) it will
83 * return the 'best' hash; otherwise the hash which was
84 * specifically requested. If no hash is found \b NULL will be returned.
85 */
86 HashString const * find(char const * const type) const;
87 HashString const * find(std::string const &type) const { return find(type.c_str()); }
88 /** check if the given hash type is supported
89 *
90 * @param type to check
91 * @return true if supported, otherwise false
92 */
93 static APT_PURE bool supported(char const * const type);
94 /** add the given #HashString to the list
95 *
96 * @param hashString to add
97 * @return true if the hash is added because it is supported and
98 * not already a different hash of the same type included, otherwise false
99 */
100 bool push_back(const HashString &hashString);
101 /** @return size of the list of HashStrings */
102 size_t size() const { return list.size(); }
103
104 /** take the 'best' hash and verify file with it
105 *
106 * @param filename to verify
107 * @return true if the file matches the hashsum, otherwise false
108 */
109 bool VerifyFile(std::string filename) const;
110
111 /** is the list empty ?
112 *
113 * @return \b true if the list is empty, otherwise \b false
114 */
115 bool empty() const { return list.empty(); }
116
117 typedef std::vector<HashString>::const_iterator const_iterator;
118
119 /** iterator to the first element */
120 const_iterator begin() const { return list.begin(); }
121
122 /** iterator to the end element */
123 const_iterator end() const { return list.end(); }
124
125 /** start fresh with a clear list */
126 void clear() { list.clear(); }
127
128 /** compare two HashStringList for similarity.
129 *
130 * Two lists are similar if at least one hashtype is in both lists
131 * and the hashsum matches. All hashes are checked, if one doesn't
132 * match false is returned regardless of how many matched before.
133 */
134 bool operator==(HashStringList const &other) const;
135 bool operator!=(HashStringList const &other) const;
136
137 HashStringList() {}
138
139 // simplifying API-compatibility constructors
140 HashStringList(std::string const &hash) {
141 if (hash.empty() == false)
142 list.push_back(HashString(hash));
143 }
144 HashStringList(char const * const hash) {
145 if (hash != NULL && hash[0] != '\0')
146 list.push_back(HashString(hash));
147 }
148
149 private:
150 std::vector<HashString> list;
151 };
152
153 class Hashes
154 {
155 public:
156
157 MD5Summation MD5;
158 SHA1Summation SHA1;
159 SHA256Summation SHA256;
160 SHA512Summation SHA512;
161
162 static const int UntilEOF = 0;
163
164 inline bool Add(const unsigned char *Data,unsigned long long Size)
165 {
166 return MD5.Add(Data,Size) && SHA1.Add(Data,Size) && SHA256.Add(Data,Size) && SHA512.Add(Data,Size);
167 };
168 inline bool Add(const char *Data) {return Add((unsigned char const *)Data,strlen(Data));};
169 inline bool AddFD(int const Fd,unsigned long long Size = 0)
170 { return AddFD(Fd, Size, true, true, true, true); };
171 bool AddFD(int const Fd, unsigned long long Size, bool const addMD5,
172 bool const addSHA1, bool const addSHA256, bool const addSHA512);
173 inline bool AddFD(FileFd &Fd,unsigned long long Size = 0)
174 { return AddFD(Fd, Size, true, true, true, true); };
175 bool AddFD(FileFd &Fd, unsigned long long Size, bool const addMD5,
176 bool const addSHA1, bool const addSHA256, bool const addSHA512);
177 inline bool Add(const unsigned char *Beg,const unsigned char *End)
178 {return Add(Beg,End-Beg);};
179 };
180
181 #endif