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