| 1 | /***************************************************************************** |
| 2 | * md5.h: Calculate MD5 |
| 3 | ***************************************************************************** |
| 4 | * Copyright (C) 2011-2012 x265 project |
| 5 | * |
| 6 | * Authors: Min Chen <chenm003@163.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA. |
| 20 | * |
| 21 | * This program is also available under a commercial proprietary license. |
| 22 | * For more information, contact us at chenm003@163.com. |
| 23 | *****************************************************************************/ |
| 24 | |
| 25 | #ifndef X265_MD5_H |
| 26 | #define X265_MD5_H |
| 27 | |
| 28 | #include "common.h" |
| 29 | |
| 30 | namespace x265 { |
| 31 | //private x265 namespace |
| 32 | |
| 33 | typedef struct MD5Context |
| 34 | { |
| 35 | uint32_t buf[4]; |
| 36 | uint32_t bits[2]; |
| 37 | unsigned char in[64]; |
| 38 | } MD5Context; |
| 39 | |
| 40 | void MD5Init(MD5Context *context); |
| 41 | void MD5Update(MD5Context *context, unsigned char *buf, uint32_t len); |
| 42 | void MD5Final(MD5Context *ctx, uint8_t *digest); |
| 43 | |
| 44 | class MD5 |
| 45 | { |
| 46 | public: |
| 47 | |
| 48 | /** |
| 49 | * initialize digest state |
| 50 | */ |
| 51 | MD5() |
| 52 | { |
| 53 | MD5Init(&m_state); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * compute digest over buf of length len. |
| 58 | * multiple calls may extend the digest over more data. |
| 59 | */ |
| 60 | void update(unsigned char *buf, unsigned len) |
| 61 | { |
| 62 | MD5Update(&m_state, buf, len); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * flush any outstanding MD5 data, write the digest into digest. |
| 67 | */ |
| 68 | void finalize(unsigned char digest[16]) |
| 69 | { |
| 70 | MD5Final(&m_state, digest); |
| 71 | } |
| 72 | |
| 73 | private: |
| 74 | |
| 75 | MD5Context m_state; |
| 76 | }; |
| 77 | } |
| 78 | |
| 79 | #endif // ifndef X265_MD5_H |