NCBI C++ Toolkit Cross Reference

C++/src/util/md5.cpp


  1 /*  $Id: md5.cpp 155350 2009-03-23 14:39:37Z grichenk $
  2  * ===========================================================================
  3  *
  4  *                            PUBLIC DOMAIN NOTICE
  5  *               National Center for Biotechnology Information
  6  *
  7  *  This software/database is a "United States Government Work" under the
  8  *  terms of the United States Copyright Act.  It was written as part of
  9  *  the author's official duties as a United States Government employee and
 10  *  thus cannot be copyrighted.  This software/database is freely available
 11  *  to the public for use. The National Library of Medicine and the U.S.
 12  *  Government have not placed any restriction on its use or reproduction.
 13  *
 14  *  Although all reasonable efforts have been taken to ensure the accuracy
 15  *  and reliability of the software and data, the NLM and the U.S.
 16  *  Government do not and cannot warrant the performance or results that
 17  *  may be obtained by using this software or data. The NLM and the U.S.
 18  *  Government disclaim all warranties, express or implied, including
 19  *  warranties of performance, merchantability or fitness for any particular
 20  *  purpose.
 21  *
 22  *  Please cite the author in any work or product based on this material.
 23  *
 24  * ===========================================================================
 25  *
 26  * Author:  Aaron Ucko (C++ interface); original author unknown
 27  *
 28  * File Description:
 29  *   CMD5 - class for computing Message Digest version 5 checksums.
 30  *
 31  */
 32 
 33 #include <ncbi_pch.hpp>
 34 #include <util/md5.hpp>
 35 #include <util/util_exception.hpp>
 36 
 37 
 38 BEGIN_NCBI_SCOPE
 39 
 40 
 41 // Note: this code is harmless on little-endian machines.
 42 inline
 43 static void s_ByteReverse(unsigned char* buf, size_t longs)
 44 {
 45     Uint4 t;
 46     do {
 47         t = (Uint4) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
 48             ((unsigned) buf[1] << 8 | buf[0]);
 49         *(reinterpret_cast<Uint4*>(buf)) = t;
 50         buf += 4;
 51     } while (--longs);
 52 }
 53 
 54 
 55 // Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
 56 // initialization constants.
 57 CMD5::CMD5(void)
 58     : m_Bits(0), m_Finalized(false)
 59 {
 60     m_Buf[0] = 0x67452301;
 61     m_Buf[1] = 0xefcdab89;
 62     m_Buf[2] = 0x98badcfe;
 63     m_Buf[3] = 0x10325476;
 64 }
 65 
 66 
 67 // Update state to reflect the concatenation of another buffer full of bytes.
 68 void CMD5::Update(const char* buf, size_t length)
 69 {
 70     if ( m_Finalized ) {
 71         NCBI_THROW(CUtilException, eWrongCommand,
 72                    "attempt to update a finalized MD5 instance");
 73     }
 74 
 75     // Number of leftover bytes in m_In
 76     unsigned int tmp = (unsigned int)((m_Bits >> 3) % sizeof(m_In));
 77     
 78     // Update bit count
 79     m_Bits += length << 3;
 80 
 81     // Handle any leading odd-sized chunks
 82     if ( tmp ) {
 83         unsigned char* p = m_In + tmp;
 84 
 85         tmp = kBlockSize - tmp;
 86         if (length < tmp) {
 87             memcpy(p, buf, length);
 88             return;
 89         }
 90         memcpy(p, buf, tmp);
 91 #ifdef WORDS_BIGENDIAN
 92         s_ByteReverse(m_In, 16);
 93 #endif
 94         Transform();
 95         buf    += tmp;
 96         length -= tmp;
 97     }
 98 
 99     // Process remaining data in kBlockSize-byte chunks
100     while (length >= kBlockSize) {
101         memcpy(m_In, buf, kBlockSize);
102 #ifdef WORDS_BIGENDIAN
103         s_ByteReverse(m_In, 16);
104 #endif
105         Transform();
106         buf    += kBlockSize;
107         length -= kBlockSize;
108     }
109 
110     // Handle any remaining bytes of data
111     memcpy(m_In, buf, length);
112 }
113 
114 
115 // Final wrapup - pad to kBlockSize-byte boundary with the bit pattern
116 // 1 0* (64-bit count of bits processed, MSB-first).
117 void CMD5::Finalize(unsigned char digest[16])
118 {
119     if ( m_Finalized ) {
120         memcpy(digest, m_Buf, 16);
121         return;
122     }
123 
124     // Compute number of bytes mod kBlockSize
125     int count = (int)((m_Bits >> 3) % kBlockSize);
126 
127     // Set the first char of padding to 0x80.  This is safe since there is
128     // always at least one byte free.
129     unsigned char *p = m_In + count;
130     *p++ = 0x80;
131 
132     // Bytes of padding needed to make kBlockSize bytes
133     count = kBlockSize - 1 - count;
134 
135     // Pad out to 56 mod kBlockSize
136     if (count < 8) {
137         // Two lots of padding:  Pad the first block to kBlockSize bytes
138         memset(p, 0, count);
139 #ifdef WORDS_BIGENDIAN
140         s_ByteReverse(m_In, 16);
141 #endif
142         Transform();
143 
144         // Now fill the next block with 56 bytes
145         memset(m_In, 0, kBlockSize - 8);
146     } else {
147         // Pad block to 56 bytes
148         memset(p, 0, count - 8);
149 #ifdef WORDS_BIGENDIAN
150         s_ByteReverse(m_In, 14);
151 #endif
152     }
153 
154     // Append length in bits and transform
155     reinterpret_cast<Uint4*>(m_In)[14] = static_cast<Uint4>(m_Bits);
156     reinterpret_cast<Uint4*>(m_In)[15] = static_cast<Uint4>(m_Bits >> 32);
157 
158     Transform();
159 #ifdef WORDS_BIGENDIAN
160     s_ByteReverse(reinterpret_cast<unsigned char*>(m_Buf), 4);
161 #endif
162     memcpy(digest, m_Buf, 16);
163     memset(m_In, 0, kBlockSize); // may be sensitive
164     m_Finalized = true;
165 }
166 
167 
168 string CMD5::GetHexSum(unsigned char digest[16])
169 {
170     CNcbiOstrstream oss;
171     for (size_t i = 0; i < 16; ++i) {
172         oss << hex << setw(2) << setfill('0') << (int)digest[i];
173     }
174     return CNcbiOstrstreamToString(oss);
175 }
176 
177 
178 // The four core functions - F1 is optimized somewhat
179 
180 // #define F1(x, y, z) (x & y | ~x & z)
181 #define F1(x, y, z) (z ^ (x & (y ^ z)))
182 #define F2(x, y, z) F1(z, x, y)
183 #define F3(x, y, z) (x ^ y ^ z)
184 #define F4(x, y, z) (y ^ (x | ~z))
185 
186 // This is the central step in the MD5 algorithm.
187 #define MD5STEP(f, w, x, y, z, data, s) \
188         ( w += f(x, y, z) + data,  w = w<<s | w>>(32-s),  w += x )
189 
190 // The core of the MD5 algorithm, this alters an existing MD5 hash to
191 // reflect the addition of 16 longwords of new data.  MD5Update blocks
192 // the data and converts bytes into longwords for this routine.
193 void CMD5::Transform(void)
194 {
195     Uint4  a, b, c, d;
196     Uint4* inw = reinterpret_cast<Uint4*>(m_In);
197 
198     a = m_Buf[0];
199     b = m_Buf[1];
200     c = m_Buf[2];
201     d = m_Buf[3];
202 
203     MD5STEP(F1, a, b, c, d, inw[0]  + 0xd76aa478,  7);
204     MD5STEP(F1, d, a, b, c, inw[1]  + 0xe8c7b756, 12);
205     MD5STEP(F1, c, d, a, b, inw[2]  + 0x242070db, 17);
206     MD5STEP(F1, b, c, d, a, inw[3]  + 0xc1bdceee, 22);
207     MD5STEP(F1, a, b, c, d, inw[4]  + 0xf57c0faf,  7);
208     MD5STEP(F1, d, a, b, c, inw[5]  + 0x4787c62a, 12);
209     MD5STEP(F1, c, d, a, b, inw[6]  + 0xa8304613, 17);
210     MD5STEP(F1, b, c, d, a, inw[7]  + 0xfd469501, 22);
211     MD5STEP(F1, a, b, c, d, inw[8]  + 0x698098d8,  7);
212     MD5STEP(F1, d, a, b, c, inw[9]  + 0x8b44f7af, 12);
213     MD5STEP(F1, c, d, a, b, inw[10] + 0xffff5bb1, 17);
214     MD5STEP(F1, b, c, d, a, inw[11] + 0x895cd7be, 22);
215     MD5STEP(F1, a, b, c, d, inw[12] + 0x6b901122,  7);
216     MD5STEP(F1, d, a, b, c, inw[13] + 0xfd987193, 12);
217     MD5STEP(F1, c, d, a, b, inw[14] + 0xa679438e, 17);
218     MD5STEP(F1, b, c, d, a, inw[15] + 0x49b40821, 22);
219 
220     MD5STEP(F2, a, b, c, d, inw[1]  + 0xf61e2562,  5);
221     MD5STEP(F2, d, a, b, c, inw[6]  + 0xc040b340,  9);
222     MD5STEP(F2, c, d, a, b, inw[11] + 0x265e5a51, 14);
223     MD5STEP(F2, b, c, d, a, inw[0]  + 0xe9b6c7aa, 20);
224     MD5STEP(F2, a, b, c, d, inw[5]  + 0xd62f105d,  5);
225     MD5STEP(F2, d, a, b, c, inw[10] + 0x02441453,  9);
226     MD5STEP(F2, c, d, a, b, inw[15] + 0xd8a1e681, 14);
227     MD5STEP(F2, b, c, d, a, inw[4]  + 0xe7d3fbc8, 20);
228     MD5STEP(F2, a, b, c, d, inw[9]  + 0x21e1cde6,  5);
229     MD5STEP(F2, d, a, b, c, inw[14] + 0xc33707d6,  9);
230     MD5STEP(F2, c, d, a, b, inw[3]  + 0xf4d50d87, 14);
231     MD5STEP(F2, b, c, d, a, inw[8]  + 0x455a14ed, 20);
232     MD5STEP(F2, a, b, c, d, inw[13] + 0xa9e3e905,  5);
233     MD5STEP(F2, d, a, b, c, inw[2]  + 0xfcefa3f8,  9);
234     MD5STEP(F2, c, d, a, b, inw[7]  + 0x676f02d9, 14);
235     MD5STEP(F2, b, c, d, a, inw[12] + 0x8d2a4c8a, 20);
236 
237     MD5STEP(F3, a, b, c, d, inw[5]  + 0xfffa3942,  4);
238     MD5STEP(F3, d, a, b, c, inw[8]  + 0x8771f681, 11);
239     MD5STEP(F3, c, d, a, b, inw[11] + 0x6d9d6122, 16);
240     MD5STEP(F3, b, c, d, a, inw[14] + 0xfde5380c, 23);
241     MD5STEP(F3, a, b, c, d, inw[1]  + 0xa4beea44,  4);
242     MD5STEP(F3, d, a, b, c, inw[4]  + 0x4bdecfa9, 11);
243     MD5STEP(F3, c, d, a, b, inw[7]  + 0xf6bb4b60, 16);
244     MD5STEP(F3, b, c, d, a, inw[10] + 0xbebfbc70, 23);
245     MD5STEP(F3, a, b, c, d, inw[13] + 0x289b7ec6,  4);
246     MD5STEP(F3, d, a, b, c, inw[0]  + 0xeaa127fa, 11);
247     MD5STEP(F3, c, d, a, b, inw[3]  + 0xd4ef3085, 16);
248     MD5STEP(F3, b, c, d, a, inw[6]  + 0x04881d05, 23);
249     MD5STEP(F3, a, b, c, d, inw[9]  + 0xd9d4d039,  4);
250     MD5STEP(F3, d, a, b, c, inw[12] + 0xe6db99e5, 11);
251     MD5STEP(F3, c, d, a, b, inw[15] + 0x1fa27cf8, 16);
252     MD5STEP(F3, b, c, d, a, inw[2]  + 0xc4ac5665, 23);
253 
254     MD5STEP(F4, a, b, c, d, inw[0]  + 0xf4292244,  6);
255     MD5STEP(F4, d, a, b, c, inw[7]  + 0x432aff97, 10);
256     MD5STEP(F4, c, d, a, b, inw[14] + 0xab9423a7, 15);
257     MD5STEP(F4, b, c, d, a, inw[5]  + 0xfc93a039, 21);
258     MD5STEP(F4, a, b, c, d, inw[12] + 0x655b59c3,  6);
259     MD5STEP(F4, d, a, b, c, inw[3]  + 0x8f0ccc92, 10);
260     MD5STEP(F4, c, d, a, b, inw[10] + 0xffeff47d, 15);
261     MD5STEP(F4, b, c, d, a, inw[1]  + 0x85845dd1, 21);
262     MD5STEP(F4, a, b, c, d, inw[8]  + 0x6fa87e4f,  6);
263     MD5STEP(F4, d, a, b, c, inw[15] + 0xfe2ce6e0, 10);
264     MD5STEP(F4, c, d, a, b, inw[6]  + 0xa3014314, 15);
265     MD5STEP(F4, b, c, d, a, inw[13] + 0x4e0811a1, 21);
266     MD5STEP(F4, a, b, c, d, inw[4]  + 0xf7537e82,  6);
267     MD5STEP(F4, d, a, b, c, inw[11] + 0xbd3af235, 10);
268     MD5STEP(F4, c, d, a, b, inw[2]  + 0x2ad7d2bb, 15);
269     MD5STEP(F4, b, c, d, a, inw[9]  + 0xeb86d391, 21);
270 
271     m_Buf[0] += a;
272     m_Buf[1] += b;
273     m_Buf[2] += c;
274     m_Buf[3] += d;
275 }
276 
277 
278 END_NCBI_SCOPE
279 

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.