]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/hashes.cc
* apt-pkg/cdrom.{cc,h}:
[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 /*{{{*/
63b1700f 14#include <apt-pkg/hashes.h>
495e5cb2
MV
15#include <apt-pkg/fileutl.h>
16#include <apt-pkg/configuration.h>
aea7f4c8
MV
17#include <apt-pkg/macros.h>
18
63b1700f 19#include <unistd.h>
495e5cb2
MV
20#include <string>
21#include <iostream>
63b1700f
AL
22 /*}}}*/
23
495e5cb2
MV
24const char* HashString::_SupportedHashes[] =
25{
d9b9e9e2 26 "SHA512", "SHA256", "SHA1", "MD5Sum", NULL
495e5cb2
MV
27};
28
29HashString::HashString()
30{
31}
32
33HashString::HashString(string Type, string Hash) : Type(Type), Hash(Hash)
34{
35}
36
92fcbfc1 37HashString::HashString(string StringedHash) /*{{{*/
495e5cb2
MV
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(":");
8a8feb29 47 Type = StringedHash.substr(0,pos);
495e5cb2
MV
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}
92fcbfc1
DK
53 /*}}}*/
54bool HashString::VerifyFile(string filename) const /*{{{*/
495e5cb2 55{
495e5cb2
MV
56 string fileHash;
57
58 FileFd Fd(filename, FileFd::ReadOnly);
2dcf7b8f 59 if(Type == "MD5Sum")
495e5cb2 60 {
2dcf7b8f 61 MD5Summation MD5;
495e5cb2
MV
62 MD5.AddFD(Fd.Fd(), Fd.Size());
63 fileHash = (string)MD5.Result();
2dcf7b8f 64 }
495e5cb2
MV
65 else if (Type == "SHA1")
66 {
2dcf7b8f 67 SHA1Summation SHA1;
495e5cb2
MV
68 SHA1.AddFD(Fd.Fd(), Fd.Size());
69 fileHash = (string)SHA1.Result();
2dcf7b8f
DK
70 }
71 else if (Type == "SHA256")
495e5cb2 72 {
2dcf7b8f 73 SHA256Summation SHA256;
495e5cb2
MV
74 SHA256.AddFD(Fd.Fd(), Fd.Size());
75 fileHash = (string)SHA256.Result();
76 }
2dcf7b8f 77 else if (Type == "SHA512")
d9b9e9e2 78 {
2dcf7b8f 79 SHA512Summation SHA512;
d9b9e9e2
MV
80 SHA512.AddFD(Fd.Fd(), Fd.Size());
81 fileHash = (string)SHA512.Result();
82 }
495e5cb2
MV
83 Fd.Close();
84
85 if(_config->FindB("Debug::Hashes",false) == true)
86 std::clog << "HashString::VerifyFile: got: " << fileHash << " expected: " << toStr() << std::endl;
87
88 return (fileHash == Hash);
89}
92fcbfc1 90 /*}}}*/
495e5cb2
MV
91const char** HashString::SupportedHashes()
92{
93 return _SupportedHashes;
94}
95
96bool HashString::empty() const
97{
98 return (Type.empty() || Hash.empty());
99}
100
495e5cb2
MV
101string HashString::toStr() const
102{
103 return Type+string(":")+Hash;
104}
105
63b1700f
AL
106// Hashes::AddFD - Add the contents of the FD /*{{{*/
107// ---------------------------------------------------------------------
108/* */
1dab797c
DK
109bool Hashes::AddFD(int const Fd,unsigned long Size, bool const addMD5,
110 bool const addSHA1, bool const addSHA256, bool const addSHA512)
63b1700f
AL
111{
112 unsigned char Buf[64*64];
113 int Res = 0;
04f4e1a3
JAK
114 int ToEOF = (Size == 0);
115 while (Size != 0 || ToEOF)
63b1700f 116 {
04f4e1a3
JAK
117 unsigned n = sizeof(Buf);
118 if (!ToEOF) n = min(Size,(unsigned long)n);
119 Res = read(Fd,Buf,n);
120 if (Res < 0 || (!ToEOF && (unsigned) Res != n)) // error, or short read
1dab797c 121 return false;
04f4e1a3 122 if (ToEOF && Res == 0) // EOF
1dab797c 123 break;
63b1700f 124 Size -= Res;
1dab797c
DK
125 if (addMD5 == true)
126 MD5.Add(Buf,Res);
127 if (addSHA1 == true)
128 SHA1.Add(Buf,Res);
129 if (addSHA256 == true)
130 SHA256.Add(Buf,Res);
131 if (addSHA512 == true)
132 SHA512.Add(Buf,Res);
63b1700f
AL
133 }
134 return true;
135}
136 /*}}}*/
137