]>
Commit | Line | Data |
---|---|---|
63b1700f AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
3 | // $Id: hashes.cc,v 1.1 2001/03/06 07:15:29 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 | // Include Files /*{{{*/ | |
63b1700f | 14 | #include <apt-pkg/hashes.h> |
495e5cb2 MV |
15 | #include <apt-pkg/fileutl.h> |
16 | #include <apt-pkg/configuration.h> | |
63b1700f AL |
17 | |
18 | #include <unistd.h> | |
19 | #include <system.h> | |
495e5cb2 MV |
20 | #include <string> |
21 | #include <iostream> | |
63b1700f AL |
22 | /*}}}*/ |
23 | ||
495e5cb2 MV |
24 | const char* HashString::_SupportedHashes[] = |
25 | { | |
26 | "SHA256", "SHA1", "MD5Sum", NULL | |
27 | }; | |
28 | ||
29 | HashString::HashString() | |
30 | { | |
31 | } | |
32 | ||
33 | HashString::HashString(string Type, string Hash) : Type(Type), Hash(Hash) | |
34 | { | |
35 | } | |
36 | ||
37 | HashString::HashString(string StringedHash) | |
38 | { | |
39 | // legacy: md5sum without "MD5Sum:" prefix | |
40 | if (StringedHash.find(":") == string::npos && StringedHash.size() == 32) | |
41 | { | |
42 | Type = "MD5Sum"; | |
43 | Hash = StringedHash; | |
44 | return; | |
45 | } | |
46 | string::size_type pos = StringedHash.find(":"); | |
47 | Type = StringedHash.substr(0,pos-1); | |
48 | Hash = StringedHash.substr(pos+1, StringedHash.size() - pos); | |
49 | ||
50 | if(_config->FindB("Debug::Hashes",false) == true) | |
51 | std::clog << "HashString(string): " << Type << " : " << Hash << std::endl; | |
52 | } | |
53 | ||
54 | ||
55 | bool HashString::VerifyFile(string filename) const | |
56 | { | |
57 | FileFd fd; | |
58 | MD5Summation MD5; | |
59 | SHA1Summation SHA1; | |
60 | SHA256Summation SHA256; | |
61 | string fileHash; | |
62 | ||
63 | FileFd Fd(filename, FileFd::ReadOnly); | |
64 | if(Type == "MD5Sum") | |
65 | { | |
66 | MD5.AddFD(Fd.Fd(), Fd.Size()); | |
67 | fileHash = (string)MD5.Result(); | |
68 | } | |
69 | else if (Type == "SHA1") | |
70 | { | |
71 | SHA1.AddFD(Fd.Fd(), Fd.Size()); | |
72 | fileHash = (string)SHA1.Result(); | |
73 | } | |
74 | else if (Type == "SHA256") | |
75 | { | |
76 | SHA256.AddFD(Fd.Fd(), Fd.Size()); | |
77 | fileHash = (string)SHA256.Result(); | |
78 | } | |
79 | Fd.Close(); | |
80 | ||
81 | if(_config->FindB("Debug::Hashes",false) == true) | |
82 | std::clog << "HashString::VerifyFile: got: " << fileHash << " expected: " << toStr() << std::endl; | |
83 | ||
84 | return (fileHash == Hash); | |
85 | } | |
86 | ||
87 | const char** HashString::SupportedHashes() | |
88 | { | |
89 | return _SupportedHashes; | |
90 | } | |
91 | ||
92 | bool HashString::empty() const | |
93 | { | |
94 | return (Type.empty() || Hash.empty()); | |
95 | } | |
96 | ||
97 | ||
98 | string HashString::toStr() const | |
99 | { | |
100 | return Type+string(":")+Hash; | |
101 | } | |
102 | ||
103 | ||
63b1700f AL |
104 | // Hashes::AddFD - Add the contents of the FD /*{{{*/ |
105 | // --------------------------------------------------------------------- | |
106 | /* */ | |
107 | bool Hashes::AddFD(int Fd,unsigned long Size) | |
108 | { | |
109 | unsigned char Buf[64*64]; | |
110 | int Res = 0; | |
111 | while (Size != 0) | |
112 | { | |
42ab8223 MV |
113 | Res = read(Fd,Buf,min(Size,(unsigned long)sizeof(Buf))); |
114 | if (Res < 0 || (unsigned)Res != min(Size,(unsigned long)sizeof(Buf))) | |
63b1700f AL |
115 | return false; |
116 | Size -= Res; | |
117 | MD5.Add(Buf,Res); | |
118 | SHA1.Add(Buf,Res); | |
cde41ae8 | 119 | SHA256.Add(Buf,Res); |
63b1700f AL |
120 | } |
121 | return true; | |
122 | } | |
123 | /*}}}*/ | |
124 |