]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/sha2.cc
00d90d6ba548d22e354cec1fdd2e1ae2a120dba2
[apt.git] / apt-pkg / contrib / sha2.cc
1 /*
2 * Cryptographic API. {{{
3 *
4 * SHA-512, as specified in
5 * http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 */ /*}}}*/
13
14 #ifdef __GNUG__
15 #pragma implementation "apt-pkg/2.h"
16 #endif
17
18 #include <apt-pkg/sha2.h>
19 #include <apt-pkg/strutl.h>
20
21 SHA512Summation::SHA512Summation() /*{{{*/
22 {
23 SHA512_Init(&ctx);
24 Done = false;
25 }
26 /*}}}*/
27 bool SHA512Summation::Add(const unsigned char *inbuf,unsigned long len) /*{{{*/
28 {
29 if (Done)
30 return false;
31 SHA512_Update(&ctx, inbuf, len);
32 return true;
33 }
34 /*}}}*/
35 SHA512SumValue SHA512Summation::Result() /*{{{*/
36 {
37 if (!Done) {
38 SHA512_Final(Sum, &ctx);
39 Done = true;
40 }
41
42 SHA512SumValue res;
43 res.Set(Sum);
44 return res;
45 }
46 /*}}}*/
47 // SHA512SumValue::SHA512SumValue - Constructs the sum from a string /*{{{*/
48 // ---------------------------------------------------------------------
49 /* The string form of a SHA512 is a 64 character hex number */
50 SHA512SumValue::SHA512SumValue(string Str)
51 {
52 memset(Sum,0,sizeof(Sum));
53 Set(Str);
54 }
55 /*}}}*/
56 // SHA512SumValue::SHA512SumValue - Default constructor /*{{{*/
57 // ---------------------------------------------------------------------
58 /* Sets the value to 0 */
59 SHA512SumValue::SHA512SumValue()
60 {
61 memset(Sum,0,sizeof(Sum));
62 }
63 /*}}}*/
64 // SHA512SumValue::Set - Set the sum from a string /*{{{*/
65 // ---------------------------------------------------------------------
66 /* Converts the hex string into a set of chars */
67 bool SHA512SumValue::Set(string Str)
68 {
69 return Hex2Num(Str,Sum,sizeof(Sum));
70 }
71 /*}}}*/
72 // SHA512SumValue::Value - Convert the number into a string /*{{{*/
73 // ---------------------------------------------------------------------
74 /* Converts the set of chars into a hex string in lower case */
75 string SHA512SumValue::Value() const
76 {
77 char Conv[16] =
78 { '0','1','2','3','4','5','6','7','8','9','a','b',
79 'c','d','e','f'
80 };
81 char Result[129];
82 Result[128] = 0;
83
84 // Convert each char into two letters
85 int J = 0;
86 int I = 0;
87 for (; I != 128; J++,I += 2)
88 {
89 Result[I] = Conv[Sum[J] >> 4];
90 Result[I + 1] = Conv[Sum[J] & 0xF];
91 }
92
93 return string(Result);
94 }
95 /*}}}*/
96 // SHA512SumValue::operator == - Comparator /*{{{*/
97 // ---------------------------------------------------------------------
98 /* Call memcmp on the buffer */
99 bool SHA512SumValue::operator == (const SHA512SumValue & rhs) const
100 {
101 return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0;
102 }
103 /*}}}*/
104 // SHA512Summation::AddFD - Add content of file into the checksum /*{{{*/
105 // ---------------------------------------------------------------------
106 /* */
107 bool SHA512Summation::AddFD(int Fd,unsigned long Size)
108 {
109 unsigned char Buf[64 * 64];
110 int Res = 0;
111 int ToEOF = (Size == 0);
112 while (Size != 0 || ToEOF)
113 {
114 unsigned n = sizeof(Buf);
115 if (!ToEOF) n = min(Size,(unsigned long)n);
116 Res = read(Fd,Buf,n);
117 if (Res < 0 || (!ToEOF && (unsigned) Res != n)) // error, or short read
118 return false;
119 if (ToEOF && Res == 0) // EOF
120 break;
121 Size -= Res;
122 Add(Buf,Res);
123 }
124 return true;
125 }
126 /*}}}*/
127
128 SHA256Summation::SHA256Summation() /*{{{*/
129 {
130 SHA256_Init(&ctx);
131 Done = false;
132 }
133 /*}}}*/
134 bool SHA256Summation::Add(const unsigned char *inbuf,unsigned long len) /*{{{*/
135 {
136 if (Done)
137 return false;
138 SHA256_Update(&ctx, inbuf, len);
139 return true;
140 }
141 /*}}}*/
142 SHA256SumValue SHA256Summation::Result() /*{{{*/
143 {
144 if (!Done) {
145 SHA256_Final(Sum, &ctx);
146 Done = true;
147 }
148
149 SHA256SumValue res;
150 res.Set(Sum);
151 return res;
152 }
153 /*}}}*/
154 // SHA256SumValue::SHA256SumValue - Constructs the sum from a string /*{{{*/
155 // ---------------------------------------------------------------------
156 /* The string form of a SHA512 is a 64 character hex number */
157 SHA256SumValue::SHA256SumValue(string Str)
158 {
159 memset(Sum,0,sizeof(Sum));
160 Set(Str);
161 }
162 /*}}}*/
163 // SHA256SumValue::SHA256SumValue - Default constructor /*{{{*/
164 // ---------------------------------------------------------------------
165 /* Sets the value to 0 */
166 SHA256SumValue::SHA256SumValue()
167 {
168 memset(Sum,0,sizeof(Sum));
169 }
170 /*}}}*/
171 // SHA256SumValue::Set - Set the sum from a string /*{{{*/
172 // ---------------------------------------------------------------------
173 /* Converts the hex string into a set of chars */
174 bool SHA256SumValue::Set(string Str)
175 {
176 return Hex2Num(Str,Sum,sizeof(Sum));
177 }
178 /*}}}*/
179 // SHA256SumValue::Value - Convert the number into a string /*{{{*/
180 // ---------------------------------------------------------------------
181 /* Converts the set of chars into a hex string in lower case */
182 string SHA256SumValue::Value() const
183 {
184 char Conv[16] =
185 { '0','1','2','3','4','5','6','7','8','9','a','b',
186 'c','d','e','f'
187 };
188 char Result[129];
189 Result[128] = 0;
190
191 // Convert each char into two letters
192 int J = 0;
193 int I = 0;
194 for (; I != 128; J++,I += 2)
195 {
196 Result[I] = Conv[Sum[J] >> 4];
197 Result[I + 1] = Conv[Sum[J] & 0xF];
198 }
199
200 return string(Result);
201 }
202 /*}}}*/
203 // SHA256SumValue::operator == - Comparator /*{{{*/
204 // ---------------------------------------------------------------------
205 /* Call memcmp on the buffer */
206 bool SHA256SumValue::operator == (const SHA256SumValue & rhs) const
207 {
208 return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0;
209 }
210 /*}}}*/
211 // SHA256Summation::AddFD - Add content of file into the checksum /*{{{*/
212 // ---------------------------------------------------------------------
213 /* */
214 bool SHA256Summation::AddFD(int Fd,unsigned long Size)
215 {
216 unsigned char Buf[64 * 64];
217 int Res = 0;
218 int ToEOF = (Size == 0);
219 while (Size != 0 || ToEOF)
220 {
221 unsigned n = sizeof(Buf);
222 if (!ToEOF) n = min(Size,(unsigned long)n);
223 Res = read(Fd,Buf,n);
224 if (Res < 0 || (!ToEOF && (unsigned) Res != n)) // error, or short read
225 return false;
226 if (ToEOF && Res == 0) // EOF
227 break;
228 Size -= Res;
229 Add(Buf,Res);
230 }
231 return true;
232 }
233 /*}}}*/
234