aboutsummaryrefslogtreecommitdiff
path: root/src/stable/sha1.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/stable/sha1.cpp282
1 files changed, 141 insertions, 141 deletions
diff --git a/src/stable/sha1.cpp b/src/stable/sha1.cpp
index e6a85cc..30e5dc8 100644
--- a/src/stable/sha1.cpp
+++ b/src/stable/sha1.cpp
@@ -12,15 +12,15 @@
12#include "bu/sha1.h" 12#include "bu/sha1.h"
13 13
14Bu::Sha1::Sha1() : 14Bu::Sha1::Sha1() :
15 uH0( 0x67452301 ), 15 uH0( 0x67452301 ),
16 uH1( 0xefcdab89 ), 16 uH1( 0xefcdab89 ),
17 uH2( 0x98badcfe ), 17 uH2( 0x98badcfe ),
18 uH3( 0x10325476 ), 18 uH3( 0x10325476 ),
19 uH4( 0xc3d2e1f0 ), 19 uH4( 0xc3d2e1f0 ),
20 iUnprocessedBytes( 0 ), 20 iUnprocessedBytes( 0 ),
21 uTotalBytes( 0 ) 21 uTotalBytes( 0 )
22{ 22{
23 reset(); 23 reset();
24} 24}
25 25
26Bu::Sha1::~Sha1() 26Bu::Sha1::~Sha1()
@@ -29,13 +29,13 @@ Bu::Sha1::~Sha1()
29 29
30void Bu::Sha1::reset() 30void Bu::Sha1::reset()
31{ 31{
32 uH0 = 0x67452301; 32 uH0 = 0x67452301;
33 uH1 = 0xefcdab89; 33 uH1 = 0xefcdab89;
34 uH2 = 0x98badcfe; 34 uH2 = 0x98badcfe;
35 uH3 = 0x10325476; 35 uH3 = 0x10325476;
36 uH4 = 0xc3d2e1f0; 36 uH4 = 0xc3d2e1f0;
37 iUnprocessedBytes = 0; 37 iUnprocessedBytes = 0;
38 uTotalBytes = 0; 38 uTotalBytes = 0;
39} 39}
40 40
41void Bu::Sha1::setSalt( const Bu::String & /*sSalt*/ ) 41void Bu::Sha1::setSalt( const Bu::String & /*sSalt*/ )
@@ -44,151 +44,151 @@ void Bu::Sha1::setSalt( const Bu::String & /*sSalt*/ )
44 44
45void Bu::Sha1::addData( const void *sDataRaw, int iSize ) 45void Bu::Sha1::addData( const void *sDataRaw, int iSize )
46{ 46{
47 const unsigned char *sData = (const unsigned char *)sDataRaw; 47 const unsigned char *sData = (const unsigned char *)sDataRaw;
48 // add these bytes to the running total 48 // add these bytes to the running total
49 uTotalBytes += iSize; 49 uTotalBytes += iSize;
50 50
51 // repeat until all data is processed 51 // repeat until all data is processed
52 while( iSize > 0 ) 52 while( iSize > 0 )
53 { 53 {
54 // number of bytes required to complete block 54 // number of bytes required to complete block
55 int iNeeded = 64 - iUnprocessedBytes; 55 int iNeeded = 64 - iUnprocessedBytes;
56 56
57 // number of bytes to copy (use smaller of two) 57 // number of bytes to copy (use smaller of two)
58 int iToCopy = (iSize < iNeeded) ? iSize : iNeeded; 58 int iToCopy = (iSize < iNeeded) ? iSize : iNeeded;
59 59
60 // Copy the bytes 60 // Copy the bytes
61 memcpy( uBytes + iUnprocessedBytes, sData, iToCopy ); 61 memcpy( uBytes + iUnprocessedBytes, sData, iToCopy );
62 62
63 // Bytes have been copied 63 // Bytes have been copied
64 iSize -= iToCopy; 64 iSize -= iToCopy;
65 sData += iToCopy; 65 sData += iToCopy;
66 iUnprocessedBytes += iToCopy; 66 iUnprocessedBytes += iToCopy;
67 67
68 // there is a full block 68 // there is a full block
69 if( iUnprocessedBytes == 64 ) 69 if( iUnprocessedBytes == 64 )
70 { 70 {
71 process(); 71 process();
72 } 72 }
73 } 73 }
74} 74}
75 75
76Bu::String Bu::Sha1::getResult() 76Bu::String Bu::Sha1::getResult()
77{ 77{
78 // save the message size 78 // save the message size
79 uint32_t totalBitsL = uTotalBytes << 3; 79 uint32_t totalBitsL = uTotalBytes << 3;
80 uint32_t totalBitsH = uTotalBytes >> 29; 80 uint32_t totalBitsH = uTotalBytes >> 29;
81 81
82 // add 0x80 to the message 82 // add 0x80 to the message
83 addData( "\x80", 1 ); 83 addData( "\x80", 1 );
84 84
85 unsigned char footer[64] = { 85 unsigned char footer[64] = {
86 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 86 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
87 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 87 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
88 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 88 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
89 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 89 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
90 90
91 // block has no room for 8-byte filesize, so finish it 91 // block has no room for 8-byte filesize, so finish it
92 if( iUnprocessedBytes > 56 ) 92 if( iUnprocessedBytes > 56 )
93 addData( (char*)footer, 64 - iUnprocessedBytes); 93 addData( (char*)footer, 64 - iUnprocessedBytes);
94 94
95 // how many zeros do we need 95 // how many zeros do we need
96 int iNeededZeros = 56 - iUnprocessedBytes; 96 int iNeededZeros = 56 - iUnprocessedBytes;
97 97
98 // store file size (in bits) in big-endian format 98 // store file size (in bits) in big-endian format
99 toBigEndian( totalBitsH, footer + iNeededZeros ); 99 toBigEndian( totalBitsH, footer + iNeededZeros );
100 toBigEndian( totalBitsL, footer + iNeededZeros + 4 ); 100 toBigEndian( totalBitsL, footer + iNeededZeros + 4 );
101 101
102 // finish the final block 102 // finish the final block
103 addData( (char*)footer, iNeededZeros + 8 ); 103 addData( (char*)footer, iNeededZeros + 8 );
104 104
105 Bu::String sRet( 20 ); 105 Bu::String sRet( 20 );
106 106
107 unsigned char *digest = (unsigned char *)sRet.getStr(); 107 unsigned char *digest = (unsigned char *)sRet.getStr();
108 108
109 // copy the digest bytes 109 // copy the digest bytes
110 toBigEndian( uH0, digest ); 110 toBigEndian( uH0, digest );
111 toBigEndian( uH1, digest + 4 ); 111 toBigEndian( uH1, digest + 4 );
112 toBigEndian( uH2, digest + 8 ); 112 toBigEndian( uH2, digest + 8 );
113 toBigEndian( uH3, digest + 12 ); 113 toBigEndian( uH3, digest + 12 );
114 toBigEndian( uH4, digest + 16 ); 114 toBigEndian( uH4, digest + 16 );
115 115
116 // return the digest 116 // return the digest
117 return sRet; 117 return sRet;
118} 118}
119 119
120void Bu::Sha1::writeResult( Bu::Stream &sOut ) 120void Bu::Sha1::writeResult( Bu::Stream &sOut )
121{ 121{
122 sOut.write( getResult() ); 122 sOut.write( getResult() );
123} 123}
124 124
125void Bu::Sha1::process() 125void Bu::Sha1::process()
126{ 126{
127 int t; 127 int t;
128 uint32_t a, b, c, d, e, K, f, W[80]; 128 uint32_t a, b, c, d, e, K, f, W[80];
129 129
130 // starting values 130 // starting values
131 a = uH0; 131 a = uH0;
132 b = uH1; 132 b = uH1;
133 c = uH2; 133 c = uH2;
134 d = uH3; 134 d = uH3;
135 e = uH4; 135 e = uH4;
136 136
137 // copy and expand the message block 137 // copy and expand the message block
138 for( t = 0; t < 16; t++ ) W[t] = (uBytes[t*4] << 24) 138 for( t = 0; t < 16; t++ ) W[t] = (uBytes[t*4] << 24)
139 +(uBytes[t*4 + 1] << 16) 139 +(uBytes[t*4 + 1] << 16)
140 +(uBytes[t*4 + 2] << 8) 140 +(uBytes[t*4 + 2] << 8)
141 + uBytes[t*4 + 3]; 141 + uBytes[t*4 + 3];
142 for(; t< 80; t++ ) W[t] = lrot( W[t-3]^W[t-8]^W[t-14]^W[t-16], 1 ); 142 for(; t< 80; t++ ) W[t] = lrot( W[t-3]^W[t-8]^W[t-14]^W[t-16], 1 );
143 143
144 /* main loop */ 144 /* main loop */
145 uint32_t temp; 145 uint32_t temp;
146 for( t = 0; t < 80; t++ ) 146 for( t = 0; t < 80; t++ )
147 { 147 {
148 if( t < 20 ) { 148 if( t < 20 ) {
149 K = 0x5a827999; 149 K = 0x5a827999;
150 f = (b & c) | ((~b) & d); 150 f = (b & c) | ((~b) & d);
151 } else if( t < 40 ) { 151 } else if( t < 40 ) {
152 K = 0x6ed9eba1; 152 K = 0x6ed9eba1;
153 f = b ^ c ^ d; 153 f = b ^ c ^ d;
154 } else if( t < 60 ) { 154 } else if( t < 60 ) {
155 K = 0x8f1bbcdc; 155 K = 0x8f1bbcdc;
156 f = (b & c) | (b & d) | (c & d); 156 f = (b & c) | (b & d) | (c & d);
157 } else { 157 } else {
158 K = 0xca62c1d6; 158 K = 0xca62c1d6;
159 f = b ^ c ^ d; 159 f = b ^ c ^ d;
160 } 160 }
161 temp = lrot(a,5) + f + e + W[t] + K; 161 temp = lrot(a,5) + f + e + W[t] + K;
162 e = d; 162 e = d;
163 d = c; 163 d = c;
164 c = lrot(b,30); 164 c = lrot(b,30);
165 b = a; 165 b = a;
166 a = temp; 166 a = temp;
167 //printf( "t=%d %08x %08x %08x %08x %08x\n",t,a,b,c,d,e ); 167 //printf( "t=%d %08x %08x %08x %08x %08x\n",t,a,b,c,d,e );
168 } 168 }
169 169
170 /* add variables */ 170 /* add variables */
171 uH0 += a; 171 uH0 += a;
172 uH1 += b; 172 uH1 += b;
173 uH2 += c; 173 uH2 += c;
174 uH3 += d; 174 uH3 += d;
175 uH4 += e; 175 uH4 += e;
176 176
177 //printf( "Current: %08x %08x %08x %08x %08x\n",H0,H1,H2,H3,H4 ); 177 //printf( "Current: %08x %08x %08x %08x %08x\n",H0,H1,H2,H3,H4 );
178 /* all bytes have been processed */ 178 /* all bytes have been processed */
179 iUnprocessedBytes = 0; 179 iUnprocessedBytes = 0;
180} 180}
181 181
182uint32_t Bu::Sha1::lrot( uint32_t x, int bits ) 182uint32_t Bu::Sha1::lrot( uint32_t x, int bits )
183{ 183{
184 return (x<<bits) | (x>>(32 - bits)); 184 return (x<<bits) | (x>>(32 - bits));
185} 185}
186 186
187void Bu::Sha1::toBigEndian( uint32_t num, unsigned char* byte ) 187void Bu::Sha1::toBigEndian( uint32_t num, unsigned char* byte )
188{ 188{
189 byte[0] = (unsigned char)(num>>24); 189 byte[0] = (unsigned char)(num>>24);
190 byte[1] = (unsigned char)(num>>16); 190 byte[1] = (unsigned char)(num>>16);
191 byte[2] = (unsigned char)(num>>8); 191 byte[2] = (unsigned char)(num>>8);
192 byte[3] = (unsigned char)num; 192 byte[3] = (unsigned char)num;
193} 193}
194 194