1 // -*- mode: cpp; mode: fold -*-
3 // $Id: hashes.h,v 1.2 2001/03/11 05:30:20 jgg Exp $
4 /* ######################################################################
6 Hashes - Simple wrapper around the hash functions
8 This is just used to make building the methods simpler, this is the
9 only interface required..
11 ##################################################################### */
13 #ifndef APTPKG_HASHES_H
14 #define APTPKG_HASHES_H
17 #include <apt-pkg/md5.h>
18 #include <apt-pkg/sha1.h>
19 #include <apt-pkg/sha2.h>
20 #include <apt-pkg/macros.h>
25 #ifndef APT_8_CLEANER_HEADERS
29 #ifndef APT_10_CLEANER_HEADERS
30 #include <apt-pkg/fileutl.h>
38 // helper class that contains hash function name
45 static const char * _SupportedHashes
[10];
48 std::string
GetHashForFile(std::string filename
) const;
51 HashString(std::string Type
, std::string Hash
);
52 HashString(std::string StringedHashString
); // init from str as "type:hash"
56 std::string
HashType() const { return Type
; };
57 std::string
HashValue() const { return Hash
; };
58 APT_DEPRECATED
std::string
HashType() { return Type
; };
59 APT_DEPRECATED
std::string
HashValue() { return Hash
; };
61 // verify the given filename against the currently loaded hash
62 bool VerifyFile(std::string filename
) const;
64 // generate a hash string from the given filename
65 bool FromFile(std::string filename
);
69 std::string
toStr() const; // convert to str as "type:hash"
71 bool operator==(HashString
const &other
) const;
72 bool operator!=(HashString
const &other
) const;
74 // return the list of hashes we support
75 static APT_CONST
const char** SupportedHashes();
81 /** find best hash if no specific one is requested
83 * @param type of the checksum to return, can be \b NULL
84 * @return If type is \b NULL (or the empty string) it will
85 * return the 'best' hash; otherwise the hash which was
86 * specifically requested. If no hash is found \b NULL will be returned.
88 HashString
const * find(char const * const type
) const;
89 HashString
const * find(std::string
const &type
) const { return find(type
.c_str()); }
91 /** finds the filesize hash and returns it as number
93 * @return beware: if the size isn't known we return \b 0 here,
94 * just like we would do for an empty file. If that is a problem
95 * for you have to get the size manually out of the list.
97 unsigned long long FileSize() const;
99 /** check if the given hash type is supported
101 * @param type to check
102 * @return true if supported, otherwise false
104 static APT_PURE
bool supported(char const * const type
);
105 /** add the given #HashString to the list
107 * @param hashString to add
108 * @return true if the hash is added because it is supported and
109 * not already a different hash of the same type included, otherwise false
111 bool push_back(const HashString
&hashString
);
112 /** @return size of the list of HashStrings */
113 size_t size() const { return list
.size(); }
115 /** take the 'best' hash and verify file with it
117 * @param filename to verify
118 * @return true if the file matches the hashsum, otherwise false
120 bool VerifyFile(std::string filename
) const;
122 /** is the list empty ?
124 * @return \b true if the list is empty, otherwise \b false
126 bool empty() const { return list
.empty(); }
128 /** has the list at least one good entry
130 * similar to #empty, but handles forced hashes.
132 * @return if no hash is forced, same result as #empty,
133 * if one is forced \b true if this has is available, \b false otherwise
137 typedef std::vector
<HashString
>::const_iterator const_iterator
;
139 /** iterator to the first element */
140 const_iterator
begin() const { return list
.begin(); }
142 /** iterator to the end element */
143 const_iterator
end() const { return list
.end(); }
145 /** start fresh with a clear list */
146 void clear() { list
.clear(); }
148 /** compare two HashStringList for similarity.
150 * Two lists are similar if at least one hashtype is in both lists
151 * and the hashsum matches. All hashes are checked by default,
152 * if one doesn't match false is returned regardless of how many
153 * matched before. If a hash is forced, only this hash is compared,
154 * all others are ignored.
156 bool operator==(HashStringList
const &other
) const;
157 bool operator!=(HashStringList
const &other
) const;
161 // simplifying API-compatibility constructors
162 HashStringList(std::string
const &hash
) {
163 if (hash
.empty() == false)
164 list
.push_back(HashString(hash
));
166 HashStringList(char const * const hash
) {
167 if (hash
!= NULL
&& hash
[0] != '\0')
168 list
.push_back(HashString(hash
));
172 std::vector
<HashString
> list
;
181 /* those will disappear in the future as it is hard to add new ones this way.
182 * Use Add* to build the results and get them via GetHashStringList() instead */
183 APT_DEPRECATED MD5Summation MD5
;
184 APT_DEPRECATED SHA1Summation SHA1
;
185 APT_DEPRECATED SHA256Summation SHA256
;
186 APT_DEPRECATED SHA512Summation SHA512
;
188 static const int UntilEOF
= 0;
190 bool Add(const unsigned char * const Data
, unsigned long long const Size
);
191 APT_DEPRECATED
bool Add(const unsigned char * const Data
, unsigned long long const Size
, unsigned int const Hashes
);
192 inline bool Add(const char * const Data
)
193 {return Add((unsigned char const * const)Data
,strlen(Data
));};
194 inline bool Add(const unsigned char * const Beg
,const unsigned char * const End
)
195 {return Add(Beg
,End
-Beg
);};
197 enum SupportedHashes
{ MD5SUM
= (1 << 0), SHA1SUM
= (1 << 1), SHA256SUM
= (1 << 2),
198 SHA512SUM
= (1 << 3) };
199 bool AddFD(int const Fd
,unsigned long long Size
= 0);
200 APT_DEPRECATED
bool AddFD(int const Fd
,unsigned long long Size
, unsigned int const Hashes
);
201 bool AddFD(FileFd
&Fd
,unsigned long long Size
= 0);
202 APT_DEPRECATED
bool AddFD(FileFd
&Fd
,unsigned long long Size
, unsigned int const Hashes
);
204 HashStringList
GetHashStringList();
206 APT_IGNORE_DEPRECATED_PUSH
207 /** create a Hashes object to calculate all supported hashes
209 * If ALL is too much, you can limit which Hashes are calculated
210 * with the following other constructors which mention explicitly
211 * which hashes to generate. */
213 /** @param Hashes bitflag composed of #SupportedHashes */
214 Hashes(unsigned int const Hashes
);
215 /** @param Hashes is a list of hashes */
216 Hashes(HashStringList
const &Hashes
);
218 APT_IGNORE_DEPRECATED_POP
221 APT_HIDDEN APT_CONST
inline unsigned int boolsToFlag(bool const addMD5
, bool const addSHA1
, bool const addSHA256
, bool const addSHA512
)
223 unsigned int Hashes
= ~0;
224 if (addMD5
== false) Hashes
&= ~MD5SUM
;
225 if (addSHA1
== false) Hashes
&= ~SHA1SUM
;
226 if (addSHA256
== false) Hashes
&= ~SHA256SUM
;
227 if (addSHA512
== false) Hashes
&= ~SHA512SUM
;
232 APT_IGNORE_DEPRECATED_PUSH
233 APT_DEPRECATED
bool AddFD(int const Fd
, unsigned long long Size
, bool const addMD5
,
234 bool const addSHA1
, bool const addSHA256
, bool const addSHA512
) {
235 return AddFD(Fd
, Size
, boolsToFlag(addMD5
, addSHA1
, addSHA256
, addSHA512
));
237 APT_DEPRECATED
bool AddFD(FileFd
&Fd
, unsigned long long Size
, bool const addMD5
,
238 bool const addSHA1
, bool const addSHA256
, bool const addSHA512
) {
239 return AddFD(Fd
, Size
, boolsToFlag(addMD5
, addSHA1
, addSHA256
, addSHA512
));
241 APT_IGNORE_DEPRECATED_POP