]>
Commit | Line | Data |
---|---|---|
63b1700f AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
678bc33e | 3 | // $Id: hashes.h,v 1.2 2001/03/11 05:30:20 jgg Exp $ |
63b1700f AL |
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 | ||
63b1700f AL |
16 | |
17 | #include <apt-pkg/md5.h> | |
18 | #include <apt-pkg/sha1.h> | |
84a0890e | 19 | #include <apt-pkg/sha2.h> |
109eb151 | 20 | #include <apt-pkg/fileutl.h> |
63b1700f | 21 | |
42ab8223 | 22 | #include <algorithm> |
495e5cb2 | 23 | #include <vector> |
b16c2617 | 24 | #include <cstring> |
42ab8223 | 25 | |
a4f6bdc8 DK |
26 | |
27 | #ifndef APT_8_CLEANER_HEADERS | |
28 | using std::min; | |
29 | using std::vector; | |
30 | #endif | |
31 | ||
495e5cb2 MV |
32 | // helper class that contains hash function name |
33 | // and hash | |
34 | class HashString | |
35 | { | |
36 | protected: | |
8f3ba4e8 DK |
37 | std::string Type; |
38 | std::string Hash; | |
495e5cb2 MV |
39 | static const char * _SupportedHashes[10]; |
40 | ||
41 | public: | |
8f3ba4e8 DK |
42 | HashString(std::string Type, std::string Hash); |
43 | HashString(std::string StringedHashString); // init from str as "type:hash" | |
495e5cb2 MV |
44 | HashString(); |
45 | ||
8a8feb29 | 46 | // get hash type used |
8f3ba4e8 | 47 | std::string HashType() { return Type; }; |
8a8feb29 | 48 | |
495e5cb2 | 49 | // verify the given filename against the currently loaded hash |
8f3ba4e8 | 50 | bool VerifyFile(std::string filename) const; |
495e5cb2 MV |
51 | |
52 | // helper | |
8f3ba4e8 | 53 | std::string toStr() const; // convert to str as "type:hash" |
495e5cb2 MV |
54 | bool empty() const; |
55 | ||
56 | // return the list of hashes we support | |
57 | static const char** SupportedHashes(); | |
58 | }; | |
42ab8223 | 59 | |
63b1700f AL |
60 | class Hashes |
61 | { | |
62 | public: | |
63 | ||
64 | MD5Summation MD5; | |
65 | SHA1Summation SHA1; | |
cde41ae8 | 66 | SHA256Summation SHA256; |
d9b9e9e2 | 67 | SHA512Summation SHA512; |
63b1700f | 68 | |
650faab0 | 69 | inline bool Add(const unsigned char *Data,unsigned long long Size) |
63b1700f | 70 | { |
d9b9e9e2 | 71 | return MD5.Add(Data,Size) && SHA1.Add(Data,Size) && SHA256.Add(Data,Size) && SHA512.Add(Data,Size); |
63b1700f AL |
72 | }; |
73 | inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));}; | |
650faab0 | 74 | inline bool AddFD(int const Fd,unsigned long long Size = 0) |
1dab797c | 75 | { return AddFD(Fd, Size, true, true, true, true); }; |
650faab0 | 76 | bool AddFD(int const Fd, unsigned long long Size, bool const addMD5, |
1dab797c | 77 | bool const addSHA1, bool const addSHA256, bool const addSHA512); |
109eb151 DK |
78 | inline bool AddFD(FileFd &Fd,unsigned long long Size = 0) |
79 | { return AddFD(Fd, Size, true, true, true, true); }; | |
80 | bool AddFD(FileFd &Fd, unsigned long long Size, bool const addMD5, | |
81 | bool const addSHA1, bool const addSHA256, bool const addSHA512); | |
63b1700f AL |
82 | inline bool Add(const unsigned char *Beg,const unsigned char *End) |
83 | {return Add(Beg,End-Beg);}; | |
84 | }; | |
85 | ||
86 | #endif |