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