merged from lp:~donkult/apt/experimental
[ntk/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
21 #include <algorithm>
22 #include <vector>
23 #include <cstring>
24
25
26 #ifndef APT_8_CLEANER_HEADERS
27 using std::min;
28 using std::vector;
29 #endif
30
31 // helper class that contains hash function name
32 // and hash
33 class HashString
34 {
35 protected:
36 std::string Type;
37 std::string Hash;
38 static const char * _SupportedHashes[10];
39
40 public:
41 HashString(std::string Type, std::string Hash);
42 HashString(std::string StringedHashString); // init from str as "type:hash"
43 HashString();
44
45 // get hash type used
46 std::string HashType() { return Type; };
47
48 // verify the given filename against the currently loaded hash
49 bool VerifyFile(std::string filename) const;
50
51 // helper
52 std::string toStr() const; // convert to str as "type:hash"
53 bool empty() const;
54
55 // return the list of hashes we support
56 static const char** SupportedHashes();
57 };
58
59 class Hashes
60 {
61 public:
62
63 MD5Summation MD5;
64 SHA1Summation SHA1;
65 SHA256Summation SHA256;
66 SHA512Summation SHA512;
67
68 inline bool Add(const unsigned char *Data,unsigned long long Size)
69 {
70 return MD5.Add(Data,Size) && SHA1.Add(Data,Size) && SHA256.Add(Data,Size) && SHA512.Add(Data,Size);
71 };
72 inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));};
73 inline bool AddFD(int const Fd,unsigned long long Size = 0)
74 { return AddFD(Fd, Size, true, true, true, true); };
75 bool AddFD(int const Fd, unsigned long long Size, bool const addMD5,
76 bool const addSHA1, bool const addSHA256, bool const addSHA512);
77 inline bool Add(const unsigned char *Beg,const unsigned char *End)
78 {return Add(Beg,End-Beg);};
79 };
80
81 #endif