Browse Source

add sha512 support in the client now as well

debian/1.8.y
Michael Vogt 12 years ago
parent
commit
d9b9e9e249
  1. 6
      apt-pkg/acquire-item.cc
  2. 8
      apt-pkg/acquire-method.cc
  3. 1
      apt-pkg/acquire-method.h
  4. 9
      apt-pkg/contrib/hashes.cc
  5. 4
      apt-pkg/contrib/hashes.h
  6. 10
      apt-pkg/deb/debrecords.cc
  7. 1
      apt-pkg/deb/debrecords.h
  8. 1
      apt-pkg/pkgrecords.h
  9. 1
      apt-pkg/tagfile.cc
  10. 2
      cmdline/apt-get.cc
  11. 39
      ftparchive/cachedb.cc
  12. 20
      ftparchive/cachedb.h
  13. 30
      ftparchive/writer.cc
  14. 2
      ftparchive/writer.h

6
apt-pkg/acquire-item.cc

@ -1665,6 +1665,8 @@ bool pkgAcqArchive::QueueNext()
string PkgFile = Parse.FileName();
if (ForceHash.empty() == false)
{
if(stringcasecmp(ForceHash, "sha512") == 0)
ExpectedHash = HashString("SHA512", Parse.SHA512Hash());
if(stringcasecmp(ForceHash, "sha256") == 0)
ExpectedHash = HashString("SHA256", Parse.SHA256Hash());
else if (stringcasecmp(ForceHash, "sha1") == 0)
@ -1675,7 +1677,9 @@ bool pkgAcqArchive::QueueNext()
else
{
string Hash;
if ((Hash = Parse.SHA256Hash()).empty() == false)
if ((Hash = Parse.SHA512Hash()).empty() == false)
ExpectedHash = HashString("SHA512", Hash);
else if ((Hash = Parse.SHA256Hash()).empty() == false)
ExpectedHash = HashString("SHA256", Hash);
else if ((Hash = Parse.SHA1Hash()).empty() == false)
ExpectedHash = HashString("SHA1", Hash);

8
apt-pkg/acquire-method.cc

@ -187,6 +187,8 @@ void pkgAcqMethod::URIDone(FetchResult &Res, FetchResult *Alt)
End += snprintf(End,sizeof(S)-50 - (End - S),"SHA1-Hash: %s\n",Res.SHA1Sum.c_str());
if (Res.SHA256Sum.empty() == false)
End += snprintf(End,sizeof(S)-50 - (End - S),"SHA256-Hash: %s\n",Res.SHA256Sum.c_str());
if (Res.SHA512Sum.empty() == false)
End += snprintf(End,sizeof(S)-50 - (End - S),"SHA512-Hash: %s\n",Res.SHA512Sum.c_str());
if (UsedMirror.empty() == false)
End += snprintf(End,sizeof(S)-50 - (End - S),"UsedMirror: %s\n",UsedMirror.c_str());
if (Res.GPGVOutput.size() > 0)
@ -224,7 +226,10 @@ void pkgAcqMethod::URIDone(FetchResult &Res, FetchResult *Alt)
if (Alt->SHA256Sum.empty() == false)
End += snprintf(End,sizeof(S)-50 - (End - S),"Alt-SHA256-Hash: %s\n",
Alt->SHA256Sum.c_str());
if (Alt->SHA512Sum.empty() == false)
End += snprintf(End,sizeof(S)-50 - (End - S),"Alt-SHA512-Hash: %s\n",
Alt->SHA512Sum.c_str());
if (Alt->IMSHit == true)
strcat(End,"Alt-IMS-Hit: true\n");
}
@ -500,5 +505,6 @@ void pkgAcqMethod::FetchResult::TakeHashes(Hashes &Hash)
MD5Sum = Hash.MD5.Result();
SHA1Sum = Hash.SHA1.Result();
SHA256Sum = Hash.SHA256.Result();
SHA512Sum = Hash.SHA512.Result();
}
/*}}}*/

1
apt-pkg/acquire-method.h

@ -45,6 +45,7 @@ class pkgAcqMethod
string MD5Sum;
string SHA1Sum;
string SHA256Sum;
string SHA512Sum;
vector<string> GPGVOutput;
time_t LastModified;
bool IMSHit;

9
apt-pkg/contrib/hashes.cc

@ -23,7 +23,7 @@
const char* HashString::_SupportedHashes[] =
{
"SHA256", "SHA1", "MD5Sum", NULL
"SHA512", "SHA256", "SHA1", "MD5Sum", NULL
};
HashString::HashString()
@ -57,6 +57,7 @@ bool HashString::VerifyFile(string filename) const /*{{{*/
MD5Summation MD5;
SHA1Summation SHA1;
SHA256Summation SHA256;
SHA256Summation SHA512;
string fileHash;
FileFd Fd(filename, FileFd::ReadOnly);
@ -75,6 +76,11 @@ bool HashString::VerifyFile(string filename) const /*{{{*/
SHA256.AddFD(Fd.Fd(), Fd.Size());
fileHash = (string)SHA256.Result();
}
else if (Type == "SHA512")
{
SHA512.AddFD(Fd.Fd(), Fd.Size());
fileHash = (string)SHA512.Result();
}
Fd.Close();
if(_config->FindB("Debug::Hashes",false) == true)
@ -119,6 +125,7 @@ bool Hashes::AddFD(int Fd,unsigned long Size)
MD5.Add(Buf,Res);
SHA1.Add(Buf,Res);
SHA256.Add(Buf,Res);
SHA512.Add(Buf,Res);
}
return true;
}

4
apt-pkg/contrib/hashes.h

@ -17,6 +17,7 @@
#include <apt-pkg/md5.h>
#include <apt-pkg/sha1.h>
#include <apt-pkg/sha256.h>
#include <apt-pkg/sha512.h>
#include <algorithm>
#include <vector>
@ -60,10 +61,11 @@ class Hashes
MD5Summation MD5;
SHA1Summation SHA1;
SHA256Summation SHA256;
SHA512Summation SHA512;
inline bool Add(const unsigned char *Data,unsigned long Size)
{
return MD5.Add(Data,Size) && SHA1.Add(Data,Size) && SHA256.Add(Data,Size);
return MD5.Add(Data,Size) && SHA1.Add(Data,Size) && SHA256.Add(Data,Size) && SHA512.Add(Data,Size);
};
inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));};
bool AddFD(int Fd,unsigned long Size);

10
apt-pkg/deb/debrecords.cc

@ -77,7 +77,7 @@ string debRecordParser::SHA1Hash()
return Section.FindS("SHA1");
}
/*}}}*/
// RecordParser::SHA1Hash - Return the archive hash /*{{{*/
// RecordParser::SHA256Hash - Return the archive hash /*{{{*/
// ---------------------------------------------------------------------
/* */
string debRecordParser::SHA256Hash()
@ -85,6 +85,14 @@ string debRecordParser::SHA256Hash()
return Section.FindS("SHA256");
}
/*}}}*/
// RecordParser::SHA512Hash - Return the archive hash /*{{{*/
// ---------------------------------------------------------------------
/* */
string debRecordParser::SHA512Hash()
{
return Section.FindS("SHA512");
}
/*}}}*/
// RecordParser::Maintainer - Return the maintainer email /*{{{*/
// ---------------------------------------------------------------------
/* */

1
apt-pkg/deb/debrecords.h

@ -36,6 +36,7 @@ class debRecordParser : public pkgRecords::Parser
virtual string MD5Hash();
virtual string SHA1Hash();
virtual string SHA256Hash();
virtual string SHA512Hash();
virtual string SourcePkg();
virtual string SourceVer();

1
apt-pkg/pkgrecords.h

@ -58,6 +58,7 @@ class pkgRecords::Parser /*{{{*/
virtual string MD5Hash() {return string();};
virtual string SHA1Hash() {return string();};
virtual string SHA256Hash() {return string();};
virtual string SHA512Hash() {return string();};
virtual string SourcePkg() {return string();};
virtual string SourceVer() {return string();};

1
apt-pkg/tagfile.cc

@ -457,6 +457,7 @@ static const char *iTFRewritePackageOrder[] = {
"MD5Sum",
"SHA1",
"SHA256",
"SHA512",
"MSDOS-Filename", // Obsolete
"Description",
0};

2
cmdline/apt-get.cc

@ -2239,6 +2239,8 @@ bool DoDownload(CommandLine &CmdL)
strprintf(descr, _("Downloading %s %s"), Pkg.Name(), Ver.VerStr());
// get the most appropriate hash
HashString hash;
if (rec.SHA512Hash() != "")
hash = HashString("sha512", rec.SHA512Hash());
if (rec.SHA256Hash() != "")
hash = HashString("sha256", rec.SHA256Hash());
else if (rec.SHA1Hash() != "")

39
ftparchive/cachedb.cc

@ -17,6 +17,7 @@
#include <apt-pkg/md5.h>
#include <apt-pkg/sha1.h>
#include <apt-pkg/sha256.h>
#include <apt-pkg/sha512.h>
#include <apt-pkg/strutl.h>
#include <apt-pkg/configuration.h>
@ -162,7 +163,8 @@ bool CacheDB::GetCurStat()
// ---------------------------------------------------------------------
bool CacheDB::GetFileInfo(string const &FileName, bool const &DoControl, bool const &DoContents,
bool const &GenContentsOnly, bool const &DoMD5, bool const &DoSHA1,
bool const &DoSHA256, bool const &checkMtime)
bool const &DoSHA256, bool const &DoSHA512,
bool const &checkMtime)
{
this->FileName = FileName;
@ -190,7 +192,9 @@ bool CacheDB::GetFileInfo(string const &FileName, bool const &DoControl, bool co
|| (DoContents && LoadContents(GenContentsOnly) == false)
|| (DoMD5 && GetMD5(false) == false)
|| (DoSHA1 && GetSHA1(false) == false)
|| (DoSHA256 && GetSHA256(false) == false))
|| (DoSHA256 && GetSHA256(false) == false)
|| (DoSHA512 && GetSHA512(false) == false)
)
{
delete Fd;
Fd = NULL;
@ -412,6 +416,37 @@ bool CacheDB::GetSHA256(bool const &GenOnly)
return true;
}
/*}}}*/
// CacheDB::GetSHA256 - Get the SHA256 hash /*{{{*/
// ---------------------------------------------------------------------
/* */
bool CacheDB::GetSHA512(bool const &GenOnly)
{
// Try to read the control information out of the DB.
if ((CurStat.Flags & FlSHA512) == FlSHA512)
{
if (GenOnly == true)
return true;
SHA512Res = bytes2hex(CurStat.SHA512, sizeof(CurStat.SHA512));
return true;
}
Stats.SHA512Bytes += CurStat.FileSize;
if (Fd == NULL && OpenFile() == false)
{
return false;
}
SHA512Summation SHA512;
if (Fd->Seek(0) == false || SHA512.AddFD(Fd->Fd(),CurStat.FileSize) == false)
return false;
SHA512Res = SHA512.Result();
hex2bytes(CurStat.SHA512, SHA512Res.data(), sizeof(CurStat.SHA512));
CurStat.Flags |= FlSHA512;
return true;
}
/*}}}*/
// CacheDB::Finish - Write back the cache structure /*{{{*/
// ---------------------------------------------------------------------
/* */

20
ftparchive/cachedb.h

@ -70,10 +70,13 @@ class CacheDB
bool GetMD5(bool const &GenOnly);
bool GetSHA1(bool const &GenOnly);
bool GetSHA256(bool const &GenOnly);
bool GetSHA512(bool const &GenOnly);
// Stat info stored in the DB, Fixed types since it is written to disk.
enum FlagList {FlControl = (1<<0),FlMD5=(1<<1),FlContents=(1<<2),
FlSize=(1<<3), FlSHA1=(1<<4), FlSHA256=(1<<5)};
FlSize=(1<<3), FlSHA1=(1<<4), FlSHA256=(1<<5),
FlSHA512=(1<<6)};
struct StatStore
{
uint32_t Flags;
@ -82,6 +85,7 @@ class CacheDB
uint8_t MD5[16];
uint8_t SHA1[20];
uint8_t SHA256[32];
uint8_t SHA512[64];
} CurStat;
struct StatStore OldStat;
@ -98,6 +102,7 @@ class CacheDB
string MD5Res;
string SHA1Res;
string SHA256Res;
string SHA512Res;
// Runtime statistics
struct Stats
@ -106,14 +111,21 @@ class CacheDB
double MD5Bytes;
double SHA1Bytes;
double SHA256Bytes;
double SHA512Bytes;
unsigned long Packages;
unsigned long Misses;
unsigned long DeLinkBytes;
inline void Add(const Stats &S) {
Bytes += S.Bytes; MD5Bytes += S.MD5Bytes; SHA1Bytes += S.SHA1Bytes;
Bytes += S.Bytes;
MD5Bytes += S.MD5Bytes;
SHA1Bytes += S.SHA1Bytes;
SHA256Bytes += S.SHA256Bytes;
Packages += S.Packages; Misses += S.Misses; DeLinkBytes += S.DeLinkBytes;};
SHA512Bytes += S.SHA512Bytes;
Packages += S.Packages;
Misses += S.Misses;
DeLinkBytes += S.DeLinkBytes;
};
Stats() : Bytes(0), MD5Bytes(0), SHA1Bytes(0), SHA256Bytes(0), Packages(0), Misses(0), DeLinkBytes(0) {};
} Stats;
@ -125,7 +137,7 @@ class CacheDB
bool SetFile(string const &FileName,struct stat St,FileFd *Fd);
bool GetFileInfo(string const &FileName, bool const &DoControl, bool const &DoContents, bool const &GenContentsOnly,
bool const &DoMD5, bool const &DoSHA1, bool const &DoSHA256, bool const &checkMtime = false);
bool const &DoMD5, bool const &DoSHA1, bool const &DoSHA256, bool const &DoSHA512, bool const &checkMtime = false);
bool Finish();
bool Clean();

30
ftparchive/writer.cc

@ -20,6 +20,7 @@
#include <apt-pkg/md5.h>
#include <apt-pkg/sha1.h>
#include <apt-pkg/sha256.h>
#include <apt-pkg/sha512.h>
#include <apt-pkg/deblistparser.h>
#include <sys/types.h>
@ -311,6 +312,7 @@ PackagesWriter::PackagesWriter(string const &DB,string const &Overrides,string c
DoMD5 = _config->FindB("APT::FTPArchive::MD5",true);
DoSHA1 = _config->FindB("APT::FTPArchive::SHA1",true);
DoSHA256 = _config->FindB("APT::FTPArchive::SHA256",true);
DoSHA256 = _config->FindB("APT::FTPArchive::SHA512",true);
DoAlwaysStat = _config->FindB("APT::FTPArchive::AlwaysStat", false);
DoContents = _config->FindB("APT::FTPArchive::Contents",true);
NoOverride = _config->FindB("APT::FTPArchive::NoOverrideMsg",false);
@ -365,7 +367,7 @@ bool FTWScanner::SetExts(string const &Vals)
bool PackagesWriter::DoPackage(string FileName)
{
// Pull all the data we need form the DB
if (Db.GetFileInfo(FileName, true, DoContents, true, DoMD5, DoSHA1, DoSHA256, DoAlwaysStat)
if (Db.GetFileInfo(FileName, true, DoContents, true, DoMD5, DoSHA1, DoSHA256, DoSHA512, DoAlwaysStat)
== false)
{
return false;
@ -438,6 +440,7 @@ bool PackagesWriter::DoPackage(string FileName)
SetTFRewriteData(Changes[End++], "MD5sum", Db.MD5Res.c_str());
SetTFRewriteData(Changes[End++], "SHA1", Db.SHA1Res.c_str());
SetTFRewriteData(Changes[End++], "SHA256", Db.SHA256Res.c_str());
SetTFRewriteData(Changes[End++], "SHA512", Db.SHA512Res.c_str());
SetTFRewriteData(Changes[End++], "Filename", NewFileName.c_str());
SetTFRewriteData(Changes[End++], "Priority", OverItem->Priority.c_str());
SetTFRewriteData(Changes[End++], "Status", 0);
@ -613,8 +616,10 @@ bool SourcesWriter::DoPackage(string FileName)
SHA1Summation SHA1;
SHA256Summation SHA256;
SHA512Summation SHA512;
SHA1.Add((unsigned char *)Start,BlkEnd - Start);
SHA256.Add((unsigned char *)Start,BlkEnd - Start);
SHA512.Add((unsigned char *)Start,BlkEnd - Start);
// Add an extra \n to the end, just in case
*BlkEnd++ = '\n';
@ -725,6 +730,12 @@ bool SourcesWriter::DoPackage(string FileName)
<< strippedName << "\n " << Tags.FindS("Checksums-Sha256");
string const ChecksumsSha256 = ostreamSha256.str();
std::ostringstream ostreamSha512;
if (Tags.Exists("Checksums-Sha512"))
ostreamSha512 << "\n " << string(SHA512.Result()) << " " << St.st_size << " "
<< strippedName << "\n " << Tags.FindS("Checksums-Sha512");
string const ChecksumsSha512 = ostreamSha512.str();
// Strip the DirStrip prefix from the FileName and add the PathPrefix
string NewFileName;
if (DirStrip.empty() == false &&
@ -777,6 +788,7 @@ bool SourcesWriter::DoPackage(string FileName)
SetTFRewriteData(Changes[End++],"Files",Files.c_str());
SetTFRewriteData(Changes[End++],"Checksums-Sha1",ChecksumsSha1.c_str());
SetTFRewriteData(Changes[End++],"Checksums-Sha256",ChecksumsSha256.c_str());
SetTFRewriteData(Changes[End++],"Checksums-Sha512",ChecksumsSha512.c_str());
if (Directory != "./")
SetTFRewriteData(Changes[End++],"Directory",Directory.c_str());
SetTFRewriteData(Changes[End++],"Priority",BestPrio.c_str());
@ -1010,6 +1022,10 @@ bool ReleaseWriter::DoPackage(string FileName)
SHA256.AddFD(fd.Fd(), fd.Size());
CheckSums[NewFileName].SHA256 = SHA256.Result();
SHA256Summation SHA512;
SHA256.AddFD(fd.Fd(), fd.Size());
CheckSums[NewFileName].SHA512 = SHA512.Result();
fd.Close();
return true;
@ -1052,5 +1068,17 @@ void ReleaseWriter::Finish()
(*I).second.size,
(*I).first.c_str());
}
fprintf(Output, "SHA512:\n");
for(map<string,struct CheckSum>::const_iterator I = CheckSums.begin();
I != CheckSums.end();
++I)
{
fprintf(Output, " %s %32ld %s\n",
(*I).second.SHA512.c_str(),
(*I).second.size,
(*I).first.c_str());
}
}

2
ftparchive/writer.h

@ -106,6 +106,7 @@ class PackagesWriter : public FTWScanner
bool DoMD5;
bool DoSHA1;
bool DoSHA256;
bool DoSHA512;
bool DoAlwaysStat;
bool NoOverride;
bool DoContents;
@ -195,6 +196,7 @@ protected:
string MD5;
string SHA1;
string SHA256;
string SHA512;
// Limited by FileFd::Size()
unsigned long size;
~CheckSum() {};

Loading…
Cancel
Save