]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/hashes.cc
Merge remote-tracking branch 'mvo/feature/source-deb822' into debian/experimental...
[apt.git] / apt-pkg / contrib / hashes.cc
CommitLineData
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
26const char* HashString::_SupportedHashes[] =
27{
d9b9e9e2 28 "SHA512", "SHA256", "SHA1", "MD5Sum", NULL
495e5cb2
MV
29};
30
31HashString::HashString()
32{
33}
34
8f3ba4e8 35HashString::HashString(std::string Type, std::string Hash) : Type(Type), Hash(Hash)
495e5cb2
MV
36{
37}
38
8f3ba4e8 39HashString::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 56bool 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 /*}}}*/
66bool 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 /*}}}*/
76std::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
110const char** HashString::SupportedHashes()
111{
112 return _SupportedHashes;
113}
114
115bool HashString::empty() const
116{
117 return (Type.empty() || Hash.empty());
118}
119
8f3ba4e8 120std::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 128bool 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];
650faab0 132 ssize_t Res = 0;
04f4e1a3
JAK
133 int ToEOF = (Size == 0);
134 while (Size != 0 || ToEOF)
63b1700f 135 {
650faab0 136 unsigned long long n = sizeof(Buf);
8f3ba4e8 137 if (!ToEOF) n = std::min(Size, n);
04f4e1a3 138 Res = read(Fd,Buf,n);
650faab0 139 if (Res < 0 || (!ToEOF && Res != (ssize_t) n)) // error, or short read
1dab797c 140 return false;
04f4e1a3 141 if (ToEOF && Res == 0) // EOF
1dab797c 142 break;
63b1700f 143 Size -= Res;
1dab797c
DK
144 if (addMD5 == true)
145 MD5.Add(Buf,Res);
146 if (addSHA1 == true)
147 SHA1.Add(Buf,Res);
148 if (addSHA256 == true)
149 SHA256.Add(Buf,Res);
150 if (addSHA512 == true)
151 SHA512.Add(Buf,Res);
63b1700f
AL
152 }
153 return true;
109eb151
DK
154}
155bool Hashes::AddFD(FileFd &Fd,unsigned long long Size, bool const addMD5,
156 bool const addSHA1, bool const addSHA256, bool const addSHA512)
157{
158 unsigned char Buf[64*64];
159 bool const ToEOF = (Size == 0);
160 while (Size != 0 || ToEOF)
161 {
162 unsigned long long n = sizeof(Buf);
163 if (!ToEOF) n = std::min(Size, n);
164 unsigned long long a = 0;
165 if (Fd.Read(Buf, n, &a) == false) // error
166 return false;
167 if (ToEOF == false)
168 {
169 if (a != n) // short read
170 return false;
171 }
172 else if (a == 0) // EOF
173 break;
174 Size -= a;
175 if (addMD5 == true)
176 MD5.Add(Buf, a);
177 if (addSHA1 == true)
178 SHA1.Add(Buf, a);
179 if (addSHA256 == true)
180 SHA256.Add(Buf, a);
181 if (addSHA512 == true)
182 SHA512.Add(Buf, a);
183 }
184 return true;
63b1700f
AL
185}
186 /*}}}*/