]>
Commit | Line | Data |
---|---|---|
54ce88fd MV |
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/sha512.h" | |
16 | #endif | |
17 | ||
18 | #include <apt-pkg/sha512.h> | |
19 | #include <apt-pkg/sha2.h> | |
20 | #include <apt-pkg/strutl.h> | |
21 | ||
22 | SHA512Summation::SHA512Summation() /*{{{*/ | |
23 | { | |
24 | SHA512_Init(&ctx); | |
25 | Done = false; | |
26 | } | |
27 | /*}}}*/ | |
28 | bool SHA512Summation::Add(const unsigned char *inbuf,unsigned long len) /*{{{*/ | |
29 | { | |
30 | if (Done) | |
31 | return false; | |
32 | SHA512_Update(&ctx, inbuf, len); | |
33 | return true; | |
34 | } | |
35 | /*}}}*/ | |
36 | SHA512SumValue SHA512Summation::Result() /*{{{*/ | |
37 | { | |
38 | if (!Done) { | |
39 | SHA512_Final(Sum, &ctx); | |
40 | Done = true; | |
41 | } | |
42 | ||
43 | SHA512SumValue res; | |
44 | res.Set(Sum); | |
45 | return res; | |
46 | } | |
47 | /*}}}*/ | |
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) | |
52 | { | |
53 | memset(Sum,0,sizeof(Sum)); | |
54 | Set(Str); | |
55 | } | |
56 | /*}}}*/ | |
57 | // SHA512SumValue::SHA512SumValue - Default constructor /*{{{*/ | |
58 | // --------------------------------------------------------------------- | |
59 | /* Sets the value to 0 */ | |
60 | SHA512SumValue::SHA512SumValue() | |
61 | { | |
62 | memset(Sum,0,sizeof(Sum)); | |
63 | } | |
64 | /*}}}*/ | |
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) | |
69 | { | |
70 | return Hex2Num(Str,Sum,sizeof(Sum)); | |
71 | } | |
72 | /*}}}*/ | |
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 | |
77 | { | |
78 | char Conv[16] = | |
79 | { '0','1','2','3','4','5','6','7','8','9','a','b', | |
80 | 'c','d','e','f' | |
81 | }; | |
82 | char Result[129]; | |
83 | Result[128] = 0; | |
84 | ||
85 | // Convert each char into two letters | |
86 | int J = 0; | |
87 | int I = 0; | |
88 | for (; I != 128; J++,I += 2) | |
89 | { | |
90 | Result[I] = Conv[Sum[J] >> 4]; | |
91 | Result[I + 1] = Conv[Sum[J] & 0xF]; | |
92 | } | |
93 | ||
94 | return string(Result); | |
95 | } | |
96 | /*}}}*/ | |
97 | // SHA512SumValue::operator == - Comparator /*{{{*/ | |
98 | // --------------------------------------------------------------------- | |
99 | /* Call memcmp on the buffer */ | |
100 | bool SHA512SumValue::operator == (const SHA512SumValue & rhs) const | |
101 | { | |
102 | return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0; | |
103 | } | |
104 | /*}}}*/ | |
105 | // SHA512Summation::AddFD - Add content of file into the checksum /*{{{*/ | |
106 | // --------------------------------------------------------------------- | |
107 | /* */ | |
108 | bool SHA512Summation::AddFD(int Fd,unsigned long Size) | |
109 | { | |
110 | unsigned char Buf[64 * 64]; | |
111 | int Res = 0; | |
112 | int ToEOF = (Size == 0); | |
113 | while (Size != 0 || ToEOF) | |
114 | { | |
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 | |
119 | return false; | |
120 | if (ToEOF && Res == 0) // EOF | |
121 | break; | |
122 | Size -= Res; | |
123 | Add(Buf,Res); | |
124 | } | |
125 | return true; | |
126 | } | |
127 | /*}}}*/ | |
128 |