merged from debian-apt
[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 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 SHA512Summation SHA512;
64
65 inline bool Add(const unsigned char *Data,unsigned long Size)
66 {
67 return MD5.Add(Data,Size) && SHA1.Add(Data,Size) && SHA256.Add(Data,Size) && SHA512.Add(Data,Size);
68 };
69 inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));};
70 inline bool AddFD(int const Fd,unsigned long Size = 0)
71 { return AddFD(Fd, Size, true, true, true, true); };
72 bool AddFD(int const Fd, unsigned long Size, bool const addMD5,
73 bool const addSHA1, bool const addSHA256, bool const addSHA512);
74 inline bool Add(const unsigned char *Beg,const unsigned char *End)
75 {return Add(Beg,End-Beg);};
76 };
77
78 #endif