]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/sha2.cc
00d90d6ba548d22e354cec1fdd2e1ae2a120dba2
2 * Cryptographic API. {{{
4 * SHA-512, as specified in
5 * http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf
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)
15 #pragma implementation "apt-pkg/2.h"
18 #include <apt-pkg/sha2.h>
19 #include <apt-pkg/strutl.h>
21 SHA512Summation::SHA512Summation() /*{{{*/
27 bool SHA512Summation::Add(const unsigned char *inbuf
,unsigned long len
) /*{{{*/
31 SHA512_Update(&ctx
, inbuf
, len
);
35 SHA512SumValue
SHA512Summation::Result() /*{{{*/
38 SHA512_Final(Sum
, &ctx
);
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
)
52 memset(Sum
,0,sizeof(Sum
));
56 // SHA512SumValue::SHA512SumValue - Default constructor /*{{{*/
57 // ---------------------------------------------------------------------
58 /* Sets the value to 0 */
59 SHA512SumValue::SHA512SumValue()
61 memset(Sum
,0,sizeof(Sum
));
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
)
69 return Hex2Num(Str
,Sum
,sizeof(Sum
));
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
78 { '0','1','2','3','4','5','6','7','8','9','a','b',
84 // Convert each char into two letters
87 for (; I
!= 128; J
++,I
+= 2)
89 Result
[I
] = Conv
[Sum
[J
] >> 4];
90 Result
[I
+ 1] = Conv
[Sum
[J
] & 0xF];
93 return string(Result
);
96 // SHA512SumValue::operator == - Comparator /*{{{*/
97 // ---------------------------------------------------------------------
98 /* Call memcmp on the buffer */
99 bool SHA512SumValue::operator == (const SHA512SumValue
& rhs
) const
101 return memcmp(Sum
,rhs
.Sum
,sizeof(Sum
)) == 0;
104 // SHA512Summation::AddFD - Add content of file into the checksum /*{{{*/
105 // ---------------------------------------------------------------------
107 bool SHA512Summation::AddFD(int Fd
,unsigned long Size
)
109 unsigned char Buf
[64 * 64];
111 int ToEOF
= (Size
== 0);
112 while (Size
!= 0 || ToEOF
)
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
119 if (ToEOF
&& Res
== 0) // EOF
128 SHA256Summation::SHA256Summation() /*{{{*/
134 bool SHA256Summation::Add(const unsigned char *inbuf
,unsigned long len
) /*{{{*/
138 SHA256_Update(&ctx
, inbuf
, len
);
142 SHA256SumValue
SHA256Summation::Result() /*{{{*/
145 SHA256_Final(Sum
, &ctx
);
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
)
159 memset(Sum
,0,sizeof(Sum
));
163 // SHA256SumValue::SHA256SumValue - Default constructor /*{{{*/
164 // ---------------------------------------------------------------------
165 /* Sets the value to 0 */
166 SHA256SumValue::SHA256SumValue()
168 memset(Sum
,0,sizeof(Sum
));
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
)
176 return Hex2Num(Str
,Sum
,sizeof(Sum
));
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
185 { '0','1','2','3','4','5','6','7','8','9','a','b',
191 // Convert each char into two letters
194 for (; I
!= 128; J
++,I
+= 2)
196 Result
[I
] = Conv
[Sum
[J
] >> 4];
197 Result
[I
+ 1] = Conv
[Sum
[J
] & 0xF];
200 return string(Result
);
203 // SHA256SumValue::operator == - Comparator /*{{{*/
204 // ---------------------------------------------------------------------
205 /* Call memcmp on the buffer */
206 bool SHA256SumValue::operator == (const SHA256SumValue
& rhs
) const
208 return memcmp(Sum
,rhs
.Sum
,sizeof(Sum
)) == 0;
211 // SHA256Summation::AddFD - Add content of file into the checksum /*{{{*/
212 // ---------------------------------------------------------------------
214 bool SHA256Summation::AddFD(int Fd
,unsigned long Size
)
216 unsigned char Buf
[64 * 64];
218 int ToEOF
= (Size
== 0);
219 while (Size
!= 0 || ToEOF
)
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
226 if (ToEOF
&& Res
== 0) // EOF