]>
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 /*{{{*/ | |
ea542140 DK |
14 | #include <config.h> |
15 | ||
63b1700f | 16 | #include <apt-pkg/hashes.h> |
495e5cb2 MV |
17 | #include <apt-pkg/fileutl.h> |
18 | #include <apt-pkg/configuration.h> | |
aea7f4c8 MV |
19 | #include <apt-pkg/macros.h> |
20 | ||
ea542140 | 21 | #include <unistd.h> |
495e5cb2 MV |
22 | #include <string> |
23 | #include <iostream> | |
63b1700f AL |
24 | /*}}}*/ |
25 | ||
495e5cb2 MV |
26 | const char* HashString::_SupportedHashes[] = |
27 | { | |
d9b9e9e2 | 28 | "SHA512", "SHA256", "SHA1", "MD5Sum", NULL |
495e5cb2 MV |
29 | }; |
30 | ||
31 | HashString::HashString() | |
32 | { | |
33 | } | |
34 | ||
8f3ba4e8 | 35 | HashString::HashString(std::string Type, std::string Hash) : Type(Type), Hash(Hash) |
495e5cb2 MV |
36 | { |
37 | } | |
38 | ||
8f3ba4e8 | 39 | HashString::HashString(std::string StringedHash) /*{{{*/ |
495e5cb2 MV |
40 | { |
41 | // legacy: md5sum without "MD5Sum:" prefix | |
8f3ba4e8 | 42 | if (StringedHash.find(":") == std::string::npos && StringedHash.size() == 32) |
495e5cb2 MV |
43 | { |
44 | Type = "MD5Sum"; | |
45 | Hash = StringedHash; | |
46 | return; | |
47 | } | |
8f3ba4e8 | 48 | std::string::size_type pos = StringedHash.find(":"); |
8a8feb29 | 49 | Type = StringedHash.substr(0,pos); |
495e5cb2 MV |
50 | Hash = StringedHash.substr(pos+1, StringedHash.size() - pos); |
51 | ||
52 | if(_config->FindB("Debug::Hashes",false) == true) | |
53 | std::clog << "HashString(string): " << Type << " : " << Hash << std::endl; | |
54 | } | |
92fcbfc1 | 55 | /*}}}*/ |
8f3ba4e8 | 56 | bool HashString::VerifyFile(std::string filename) const /*{{{*/ |
e6645b9f MV |
57 | { |
58 | std::string fileHash = GetHashForFile(filename); | |
59 | ||
60 | if(_config->FindB("Debug::Hashes",false) == true) | |
61 | std::clog << "HashString::VerifyFile: got: " << fileHash << " expected: " << toStr() << std::endl; | |
62 | ||
63 | return (fileHash == Hash); | |
64 | } | |
65 | /*}}}*/ | |
66 | bool HashString::FromFile(std::string filename) /*{{{*/ | |
67 | { | |
68 | // pick the strongest hash | |
69 | if (Type == "") | |
70 | Type = _SupportedHashes[0]; | |
71 | ||
72 | Hash = GetHashForFile(filename); | |
73 | return true; | |
74 | } | |
75 | /*}}}*/ | |
76 | std::string HashString::GetHashForFile(std::string filename) const /*{{{*/ | |
495e5cb2 | 77 | { |
8f3ba4e8 | 78 | std::string fileHash; |
495e5cb2 MV |
79 | |
80 | FileFd Fd(filename, FileFd::ReadOnly); | |
2dcf7b8f | 81 | if(Type == "MD5Sum") |
495e5cb2 | 82 | { |
2dcf7b8f | 83 | MD5Summation MD5; |
109eb151 | 84 | MD5.AddFD(Fd); |
8f3ba4e8 | 85 | fileHash = (std::string)MD5.Result(); |
2dcf7b8f | 86 | } |
495e5cb2 MV |
87 | else if (Type == "SHA1") |
88 | { | |
2dcf7b8f | 89 | SHA1Summation SHA1; |
109eb151 | 90 | SHA1.AddFD(Fd); |
8f3ba4e8 | 91 | fileHash = (std::string)SHA1.Result(); |
2dcf7b8f DK |
92 | } |
93 | else if (Type == "SHA256") | |
495e5cb2 | 94 | { |
2dcf7b8f | 95 | SHA256Summation SHA256; |
109eb151 | 96 | SHA256.AddFD(Fd); |
8f3ba4e8 | 97 | fileHash = (std::string)SHA256.Result(); |
495e5cb2 | 98 | } |
2dcf7b8f | 99 | else if (Type == "SHA512") |
d9b9e9e2 | 100 | { |
2dcf7b8f | 101 | SHA512Summation SHA512; |
109eb151 | 102 | SHA512.AddFD(Fd); |
8f3ba4e8 | 103 | fileHash = (std::string)SHA512.Result(); |
d9b9e9e2 | 104 | } |
495e5cb2 MV |
105 | Fd.Close(); |
106 | ||
e6645b9f | 107 | return fileHash; |
495e5cb2 | 108 | } |
92fcbfc1 | 109 | /*}}}*/ |
495e5cb2 MV |
110 | const char** HashString::SupportedHashes() |
111 | { | |
112 | return _SupportedHashes; | |
113 | } | |
114 | ||
115 | bool HashString::empty() const | |
116 | { | |
117 | return (Type.empty() || Hash.empty()); | |
118 | } | |
119 | ||
8f3ba4e8 | 120 | std::string HashString::toStr() const |
495e5cb2 | 121 | { |
8f3ba4e8 | 122 | return Type + std::string(":") + Hash; |
495e5cb2 MV |
123 | } |
124 | ||
63b1700f AL |
125 | // Hashes::AddFD - Add the contents of the FD /*{{{*/ |
126 | // --------------------------------------------------------------------- | |
127 | /* */ | |
650faab0 | 128 | bool Hashes::AddFD(int const Fd,unsigned long long Size, bool const addMD5, |
1dab797c | 129 | bool const addSHA1, bool const addSHA256, bool const addSHA512) |
63b1700f AL |
130 | { |
131 | unsigned char Buf[64*64]; | |
9ce3cfc9 | 132 | bool const ToEOF = (Size == 0); |
04f4e1a3 | 133 | while (Size != 0 || ToEOF) |
63b1700f | 134 | { |
650faab0 | 135 | unsigned long long n = sizeof(Buf); |
8f3ba4e8 | 136 | if (!ToEOF) n = std::min(Size, n); |
9ce3cfc9 | 137 | ssize_t const Res = read(Fd,Buf,n); |
650faab0 | 138 | if (Res < 0 || (!ToEOF && Res != (ssize_t) n)) // error, or short read |
1dab797c | 139 | return false; |
04f4e1a3 | 140 | if (ToEOF && Res == 0) // EOF |
1dab797c | 141 | break; |
63b1700f | 142 | Size -= Res; |
1dab797c DK |
143 | if (addMD5 == true) |
144 | MD5.Add(Buf,Res); | |
145 | if (addSHA1 == true) | |
146 | SHA1.Add(Buf,Res); | |
147 | if (addSHA256 == true) | |
148 | SHA256.Add(Buf,Res); | |
149 | if (addSHA512 == true) | |
150 | SHA512.Add(Buf,Res); | |
63b1700f AL |
151 | } |
152 | return true; | |
109eb151 DK |
153 | } |
154 | bool Hashes::AddFD(FileFd &Fd,unsigned long long Size, bool const addMD5, | |
155 | bool const addSHA1, bool const addSHA256, bool const addSHA512) | |
156 | { | |
157 | unsigned char Buf[64*64]; | |
158 | bool const ToEOF = (Size == 0); | |
159 | while (Size != 0 || ToEOF) | |
160 | { | |
161 | unsigned long long n = sizeof(Buf); | |
162 | if (!ToEOF) n = std::min(Size, n); | |
163 | unsigned long long a = 0; | |
164 | if (Fd.Read(Buf, n, &a) == false) // error | |
165 | return false; | |
166 | if (ToEOF == false) | |
167 | { | |
168 | if (a != n) // short read | |
169 | return false; | |
170 | } | |
171 | else if (a == 0) // EOF | |
172 | break; | |
173 | Size -= a; | |
174 | if (addMD5 == true) | |
175 | MD5.Add(Buf, a); | |
176 | if (addSHA1 == true) | |
177 | SHA1.Add(Buf, a); | |
178 | if (addSHA256 == true) | |
179 | SHA256.Add(Buf, a); | |
180 | if (addSHA512 == true) | |
181 | SHA512.Add(Buf, a); | |
182 | } | |
183 | return true; | |
63b1700f AL |
184 | } |
185 | /*}}}*/ |