libsidplayfp  1.0.3
MD5.h
00001 /*
00002  * This code has been derived by Michael Schwendt <mschwendt@yahoo.com>
00003  * from original work by L. Peter Deutsch <ghost@aladdin.com>.
00004  * 
00005  * The original C code (md5.c, md5.h) is available here:
00006  * ftp://ftp.cs.wisc.edu/ghost/packages/md5.tar.gz
00007  */
00008 
00009 /*
00010   Copyright (C) 1999 Aladdin Enterprises.  All rights reserved.
00011 
00012   This software is provided 'as-is', without any express or implied
00013   warranty.  In no event will the authors be held liable for any damages
00014   arising from the use of this software.
00015 
00016   Permission is granted to anyone to use this software for any purpose,
00017   including commercial applications, and to alter it and redistribute it
00018   freely, subject to the following restrictions:
00019 
00020   1. The origin of this software must not be misrepresented; you must not
00021      claim that you wrote the original software. If you use this software
00022      in a product, an acknowledgment in the product documentation would be
00023      appreciated but is not required.
00024   2. Altered source versions must be plainly marked as such, and must not be
00025      misrepresented as being the original software.
00026   3. This notice may not be removed or altered from any source distribution.
00027 
00028   L. Peter Deutsch
00029   ghost@aladdin.com
00030  */
00031 
00032 #ifndef MD5_H
00033 #define MD5_H
00034 
00035 #include <stdint.h>
00036 
00037 #include "MD5_Defs.h"
00038 
00039 typedef uint8_t md5_byte_t;
00040 typedef uint32_t md5_word_t;
00041 
00042 class MD5
00043 {
00044  public:
00045     // Initialize the algorithm. Reset starting values.
00046     MD5();
00047 
00048     // Append a string to the message.
00049     void append(const void* data, int nbytes);
00050 
00051     // Finish the message.
00052     void finish();
00053 
00054     // Return pointer to 16-byte fingerprint.
00055     const md5_byte_t* getDigest();
00056 
00057     // Initialize the algorithm. Reset starting values.
00058     void reset();
00059 
00060  private:
00061 
00062     /* Define the state of the MD5 Algorithm. */
00063     md5_word_t count[2];        /* message length in bits, lsw first */
00064     md5_word_t abcd[4];         /* digest buffer */
00065     md5_byte_t buf[64];         /* accumulate block */
00066 
00067     md5_byte_t digest[16];
00068 
00069     md5_word_t tmpBuf[16];
00070     const md5_word_t* X;
00071 
00072     void
00073     process(const md5_byte_t data[64]);
00074 
00075     md5_word_t
00076     ROTATE_LEFT(const md5_word_t x, const int n);
00077 
00078     md5_word_t
00079     F(const md5_word_t x, const md5_word_t y, const md5_word_t z);
00080 
00081     md5_word_t
00082     G(const md5_word_t x, const md5_word_t y, const md5_word_t z);
00083 
00084     md5_word_t
00085     H(const md5_word_t x, const md5_word_t y, const md5_word_t z);
00086 
00087     md5_word_t
00088     I(const md5_word_t x, const md5_word_t y, const md5_word_t z);
00089 
00090     typedef md5_word_t (MD5::*md5func)(const md5_word_t x, const md5_word_t y, const md5_word_t z);
00091 
00092     void
00093     SET(md5func func, md5_word_t& a, md5_word_t& b, md5_word_t& c,
00094         md5_word_t& d, const int k, const int s,
00095         const md5_word_t Ti);
00096 };
00097 
00098 inline md5_word_t
00099 MD5::ROTATE_LEFT(const md5_word_t x, const int n)
00100 {
00101     return ( (x<<n) | (x>>(32-n)) );
00102 }
00103 
00104 inline md5_word_t
00105 MD5::F(const md5_word_t x, const md5_word_t y, const md5_word_t z)
00106 {
00107     return ( (x&y) | (~x&z) );
00108 }
00109 
00110 inline md5_word_t
00111 MD5::G(const md5_word_t x, const md5_word_t y, const md5_word_t z)
00112 {
00113     return ( (x&z) | (y&~z) );
00114 }
00115 
00116 inline md5_word_t
00117 MD5::H(const md5_word_t x, const md5_word_t y, const md5_word_t z)
00118 {
00119     return ( x^y^z );
00120 }
00121 
00122 inline md5_word_t
00123 MD5::I(const md5_word_t x, const md5_word_t y, const md5_word_t z)
00124 {
00125     return ( y ^ (x|~z) );
00126 }
00127 
00128 inline void
00129 MD5::SET(md5func func, md5_word_t& a, md5_word_t& b, md5_word_t& c,
00130           md5_word_t& d, const int k, const int s,
00131           const md5_word_t Ti)
00132 {
00133     md5_word_t t = a + (this->*func)(b,c,d) + X[k] + Ti;
00134     a = ROTATE_LEFT(t, s) + b;
00135 }
00136 
00137 #endif  /* MD5_H */