]>
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 /*{{{*/ |
495e5cb2 | 57 | { |
8f3ba4e8 | 58 | std::string fileHash; |
495e5cb2 MV |
59 | |
60 | FileFd Fd(filename, FileFd::ReadOnly); | |
2dcf7b8f | 61 | if(Type == "MD5Sum") |
495e5cb2 | 62 | { |
2dcf7b8f | 63 | MD5Summation MD5; |
109eb151 | 64 | MD5.AddFD(Fd); |
8f3ba4e8 | 65 | fileHash = (std::string)MD5.Result(); |
2dcf7b8f | 66 | } |
495e5cb2 MV |
67 | else if (Type == "SHA1") |
68 | { | |
2dcf7b8f | 69 | SHA1Summation SHA1; |
109eb151 | 70 | SHA1.AddFD(Fd); |
8f3ba4e8 | 71 | fileHash = (std::string)SHA1.Result(); |
2dcf7b8f DK |
72 | } |
73 | else if (Type == "SHA256") | |
495e5cb2 | 74 | { |
2dcf7b8f | 75 | SHA256Summation SHA256; |
109eb151 | 76 | SHA256.AddFD(Fd); |
8f3ba4e8 | 77 | fileHash = (std::string)SHA256.Result(); |
495e5cb2 | 78 | } |
2dcf7b8f | 79 | else if (Type == "SHA512") |
d9b9e9e2 | 80 | { |
2dcf7b8f | 81 | SHA512Summation SHA512; |
109eb151 | 82 | SHA512.AddFD(Fd); |
8f3ba4e8 | 83 | fileHash = (std::string)SHA512.Result(); |
d9b9e9e2 | 84 | } |
495e5cb2 MV |
85 | Fd.Close(); |
86 | ||
87 | if(_config->FindB("Debug::Hashes",false) == true) | |
88 | std::clog << "HashString::VerifyFile: got: " << fileHash << " expected: " << toStr() << std::endl; | |
89 | ||
90 | return (fileHash == Hash); | |
91 | } | |
92fcbfc1 | 92 | /*}}}*/ |
495e5cb2 MV |
93 | const char** HashString::SupportedHashes() |
94 | { | |
95 | return _SupportedHashes; | |
96 | } | |
97 | ||
98 | bool HashString::empty() const | |
99 | { | |
100 | return (Type.empty() || Hash.empty()); | |
101 | } | |
102 | ||
8f3ba4e8 | 103 | std::string HashString::toStr() const |
495e5cb2 | 104 | { |
8f3ba4e8 | 105 | return Type + std::string(":") + Hash; |
495e5cb2 MV |
106 | } |
107 | ||
63b1700f AL |
108 | // Hashes::AddFD - Add the contents of the FD /*{{{*/ |
109 | // --------------------------------------------------------------------- | |
110 | /* */ | |
650faab0 | 111 | bool Hashes::AddFD(int const Fd,unsigned long long Size, bool const addMD5, |
1dab797c | 112 | bool const addSHA1, bool const addSHA256, bool const addSHA512) |
63b1700f AL |
113 | { |
114 | unsigned char Buf[64*64]; | |
650faab0 | 115 | ssize_t Res = 0; |
04f4e1a3 JAK |
116 | int ToEOF = (Size == 0); |
117 | while (Size != 0 || ToEOF) | |
63b1700f | 118 | { |
650faab0 | 119 | unsigned long long n = sizeof(Buf); |
8f3ba4e8 | 120 | if (!ToEOF) n = std::min(Size, n); |
04f4e1a3 | 121 | Res = read(Fd,Buf,n); |
650faab0 | 122 | if (Res < 0 || (!ToEOF && Res != (ssize_t) n)) // error, or short read |
1dab797c | 123 | return false; |
04f4e1a3 | 124 | if (ToEOF && Res == 0) // EOF |
1dab797c | 125 | break; |
63b1700f | 126 | Size -= Res; |
1dab797c DK |
127 | if (addMD5 == true) |
128 | MD5.Add(Buf,Res); | |
129 | if (addSHA1 == true) | |
130 | SHA1.Add(Buf,Res); | |
131 | if (addSHA256 == true) | |
132 | SHA256.Add(Buf,Res); | |
133 | if (addSHA512 == true) | |
134 | SHA512.Add(Buf,Res); | |
63b1700f AL |
135 | } |
136 | return true; | |
109eb151 DK |
137 | } |
138 | bool Hashes::AddFD(FileFd &Fd,unsigned long long Size, bool const addMD5, | |
139 | bool const addSHA1, bool const addSHA256, bool const addSHA512) | |
140 | { | |
141 | unsigned char Buf[64*64]; | |
142 | bool const ToEOF = (Size == 0); | |
143 | while (Size != 0 || ToEOF) | |
144 | { | |
145 | unsigned long long n = sizeof(Buf); | |
146 | if (!ToEOF) n = std::min(Size, n); | |
147 | unsigned long long a = 0; | |
148 | if (Fd.Read(Buf, n, &a) == false) // error | |
149 | return false; | |
150 | if (ToEOF == false) | |
151 | { | |
152 | if (a != n) // short read | |
153 | return false; | |
154 | } | |
155 | else if (a == 0) // EOF | |
156 | break; | |
157 | Size -= a; | |
158 | if (addMD5 == true) | |
159 | MD5.Add(Buf, a); | |
160 | if (addSHA1 == true) | |
161 | SHA1.Add(Buf, a); | |
162 | if (addSHA256 == true) | |
163 | SHA256.Add(Buf, a); | |
164 | if (addSHA512 == true) | |
165 | SHA512.Add(Buf, a); | |
166 | } | |
167 | return true; | |
63b1700f AL |
168 | } |
169 | /*}}}*/ |