]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/sha512.cc
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/sha512.h"
18 #include <apt-pkg/sha512.h>
19 #include <apt-pkg/sha2.h>
20 #include <apt-pkg/strutl.h>
22 SHA512Summation::SHA512Summation() /*{{{*/
28 bool SHA512Summation::Add(const unsigned char *inbuf
,unsigned long len
) /*{{{*/
32 SHA512_Update(&ctx
, inbuf
, len
);
36 SHA512SumValue
SHA512Summation::Result() /*{{{*/
39 SHA512_Final(Sum
, &ctx
);
48 // SHA512SumValue::SHA512SumValue - Constructs the sum from a string /*{{{*/
49 // ---------------------------------------------------------------------
50 /* The string form of a SHA512 is a 64 character hex number */
51 SHA512SumValue::SHA512SumValue(string Str
)
53 memset(Sum
,0,sizeof(Sum
));
57 // SHA512SumValue::SHA512SumValue - Default constructor /*{{{*/
58 // ---------------------------------------------------------------------
59 /* Sets the value to 0 */
60 SHA512SumValue::SHA512SumValue()
62 memset(Sum
,0,sizeof(Sum
));
65 // SHA512SumValue::Set - Set the sum from a string /*{{{*/
66 // ---------------------------------------------------------------------
67 /* Converts the hex string into a set of chars */
68 bool SHA512SumValue::Set(string Str
)
70 return Hex2Num(Str
,Sum
,sizeof(Sum
));
73 // SHA512SumValue::Value - Convert the number into a string /*{{{*/
74 // ---------------------------------------------------------------------
75 /* Converts the set of chars into a hex string in lower case */
76 string
SHA512SumValue::Value() const
79 { '0','1','2','3','4','5','6','7','8','9','a','b',
85 // Convert each char into two letters
88 for (; I
!= 128; J
++,I
+= 2)
90 Result
[I
] = Conv
[Sum
[J
] >> 4];
91 Result
[I
+ 1] = Conv
[Sum
[J
] & 0xF];
94 return string(Result
);
97 // SHA512SumValue::operator == - Comparator /*{{{*/
98 // ---------------------------------------------------------------------
99 /* Call memcmp on the buffer */
100 bool SHA512SumValue::operator == (const SHA512SumValue
& rhs
) const
102 return memcmp(Sum
,rhs
.Sum
,sizeof(Sum
)) == 0;
105 // SHA512Summation::AddFD - Add content of file into the checksum /*{{{*/
106 // ---------------------------------------------------------------------
108 bool SHA512Summation::AddFD(int Fd
,unsigned long Size
)
110 unsigned char Buf
[64 * 64];
112 int ToEOF
= (Size
== 0);
113 while (Size
!= 0 || ToEOF
)
115 unsigned n
= sizeof(Buf
);
116 if (!ToEOF
) n
= min(Size
,(unsigned long)n
);
117 Res
= read(Fd
,Buf
,n
);
118 if (Res
< 0 || (!ToEOF
&& (unsigned) Res
!= n
)) // error, or short read
120 if (ToEOF
&& Res
== 0) // EOF