1 /* Copyright (C) 2010 Wildfire Games. 2 * This file is part of 0 A.D. 3 * 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * 0 A.D. is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #ifndef INCLUDED_MD5 19 #define INCLUDED_MD5 20 21 #include <cstring> 22 23 /** 24 * MD5 hashing algorithm. Note that MD5 is broken and must not be used for 25 * anything that requires security. 26 */ 27 class MD5 28 { 29 public: 30 static const size_t DIGESTSIZE = 16; 31 32 MD5(); 33 Update(const u8 * data,size_t len)34 void Update(const u8* data, size_t len) 35 { 36 // (Defined inline for efficiency in the common fixed-length fits-in-buffer case) 37 38 const size_t CHUNK_SIZE = sizeof(m_Buf); 39 40 m_InputLen += len; 41 42 // If we have enough space in m_Buf and won't flush, simply append the input 43 if (m_BufLen + len < CHUNK_SIZE) 44 { 45 memcpy(m_Buf + m_BufLen, data, len); 46 m_BufLen += len; 47 return; 48 } 49 50 // Fall back to non-inline function if we have to do more work 51 UpdateRest(data, len); 52 } 53 54 void Final(u8* digest); 55 56 private: 57 void InitState(); 58 void UpdateRest(const u8* data, size_t len); 59 void Transform(const u32* in); 60 u32 m_Digest[4]; // internal state 61 u8 m_Buf[64]; // buffered input bytes 62 size_t m_BufLen; // bytes in m_Buf that are valid 63 u64 m_InputLen; // bytes 64 }; 65 66 #endif // INCLUDED_MD5 67