]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/hashes.h
Merge branch 'feature/popen' into feature/apt-install-deb
[apt.git] / apt-pkg / contrib / hashes.h
CommitLineData
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>
63b1700f 20
b16c2617 21#include <cstring>
453b82a3 22#include <string>
a4f6bdc8
DK
23
24#ifndef APT_8_CLEANER_HEADERS
25using std::min;
26using std::vector;
27#endif
453b82a3
DK
28#ifndef APT_10_CLEANER_HEADERS
29#include <apt-pkg/fileutl.h>
30#include <algorithm>
31#include <vector>
32#endif
33
34
35class FileFd;
a4f6bdc8 36
495e5cb2
MV
37// helper class that contains hash function name
38// and hash
39class HashString
40{
41 protected:
8f3ba4e8
DK
42 std::string Type;
43 std::string Hash;
e6645b9f
MV
44 static const char* _SupportedHashes[10];
45
46 // internal helper
47 std::string GetHashForFile(std::string filename) const;
495e5cb2
MV
48
49 public:
8f3ba4e8
DK
50 HashString(std::string Type, std::string Hash);
51 HashString(std::string StringedHashString); // init from str as "type:hash"
495e5cb2
MV
52 HashString();
53
8a8feb29 54 // get hash type used
8f3ba4e8 55 std::string HashType() { return Type; };
8a8feb29 56
495e5cb2 57 // verify the given filename against the currently loaded hash
8f3ba4e8 58 bool VerifyFile(std::string filename) const;
495e5cb2 59
e6645b9f
MV
60 // generate a hash string from the given filename
61 bool FromFile(std::string filename);
62
63
495e5cb2 64 // helper
8f3ba4e8 65 std::string toStr() const; // convert to str as "type:hash"
495e5cb2
MV
66 bool empty() const;
67
68 // return the list of hashes we support
a02db58f 69 static APT_CONST const char** SupportedHashes();
495e5cb2 70};
42ab8223 71
63b1700f
AL
72class Hashes
73{
74 public:
75
76 MD5Summation MD5;
77 SHA1Summation SHA1;
cde41ae8 78 SHA256Summation SHA256;
d9b9e9e2 79 SHA512Summation SHA512;
63b1700f 80
650faab0 81 inline bool Add(const unsigned char *Data,unsigned long long Size)
63b1700f 82 {
d9b9e9e2 83 return MD5.Add(Data,Size) && SHA1.Add(Data,Size) && SHA256.Add(Data,Size) && SHA512.Add(Data,Size);
63b1700f 84 };
cf4ff3b7 85 inline bool Add(const char *Data) {return Add((unsigned char const *)Data,strlen(Data));};
650faab0 86 inline bool AddFD(int const Fd,unsigned long long Size = 0)
1dab797c 87 { return AddFD(Fd, Size, true, true, true, true); };
650faab0 88 bool AddFD(int const Fd, unsigned long long Size, bool const addMD5,
1dab797c 89 bool const addSHA1, bool const addSHA256, bool const addSHA512);
109eb151
DK
90 inline bool AddFD(FileFd &Fd,unsigned long long Size = 0)
91 { return AddFD(Fd, Size, true, true, true, true); };
92 bool AddFD(FileFd &Fd, unsigned long long Size, bool const addMD5,
93 bool const addSHA1, bool const addSHA256, bool const addSHA512);
63b1700f
AL
94 inline bool Add(const unsigned char *Beg,const unsigned char *End)
95 {return Add(Beg,End-Beg);};
96};
97
98#endif