00001 /*!
00002 * \file MD5.h
00003 * \brief C++ implementation of MD5 Message-Digest Algorithm as describe in
00004 * RFC1321.
00005 *
00006 * Written by Ulrich Drepper <drepper@gnu.ai.mit.edu> and heavily modified
00007 * for GnuPG by <werner.koch@guug.de> and modified for the need of FPM
00008 * Blowfish Plugin.
00009 *
00010 * \author Frederic RUAUDEL <grumz@users.sf.net>
00011 *
00012 * $Revision: 1.1 $
00013 * $Date: 2003/03/07 10:54:38 $
00014 *
00015 * \b FPMBlowfishPlugin
00016 * Copyleft (c) 2003 Frederic RUAUDEL, all rights reversed
00017 *
00018 * Copyleft (C) 1995, 1996, 1998, 1999 Free Software Foundation, Inc.
00019 *
00020 * This program is free software which I release under the GNU General Public
00021 * License. You may redistribute and/or modify this program under the terms
00022 * of that license as published by the Free Software Foundation; either
00023 * version 2 of the License, or (at your option) any later version.
00024 *
00025 * This program is distributed in the hope that it will be useful,
00026 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00027 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00028 * GNU General Public License for more details. Version 2 is in the
00029 * COPYING file in the top level directory of this distribution.
00030 *
00031 * To get a copy of the GNU General Public License, write to the Free Software
00032 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00033 **/
00034
00035 #ifndef MD5_H
00036 #define MD5_H
00037
00038 /* Test values:
00039 * "" D4 1D 8C D9 8F 00 B2 04 E9 80 09 98 EC F8 42 7E
00040 * "a" 0C C1 75 B9 C0 F1 B6 A8 31 C3 99 E2 69 77 26 61
00041 * "abc 90 01 50 98 3C D2 4F B0 D6 96 3F 7D 28 E1 7F 72
00042 * "message digest" F9 6B 69 7D 7C B7 93 8D 52 5A 2F 31 AA F1 61 D0
00043 */
00044
00045 #include <iostream>
00046 #include <stdio.h>
00047 #include <stdlib.h>
00048 #include <string.h>
00049
00050 typedef unsigned int u32;
00051 typedef unsigned char byte;
00052
00053 /****************
00054 * Rotate a 32 bit integer by n bytes
00055 */
00056 #if defined(__GNUC__) && defined(__i386__)
00057 static inline u32 rol( u32 x, int n)
00058 {
00059 __asm__("roll %%cl,%0"
00060 :"=r" (x)
00061 :"0" (x),"c" (n));
00062 return x;
00063 }
00064 #else
00065 #define rol(x,n) ( ((x) << (n)) | ((x) >> (32-(n))) )
00066 #endif
00067
00068 /* These are the four functions used in the four steps of the MD5 algorithm
00069 and defined in the RFC 1321. The first function is a little bit optimized
00070 (as found in Colin Plumbs public domain implementation). */
00071 /* #define FF(b, c, d) ((b & c) | (~b & d)) */
00072 #define FF(b, c, d) (d ^ (b & (c ^ d)))
00073 #define FG(b, c, d) FF (d, b, c)
00074 #define FH(b, c, d) (b ^ c ^ d)
00075 #define FI(b, c, d) (c ^ (b | ~d))
00076
00077 using namespace std;
00078
00079 class MD5
00080 {
00081 public:
00082 MD5 ();
00083 ~MD5 ();
00084
00085 byte* hash (byte *buf, size_t nbytes);
00086 void print (byte* key, size_t nbytes, byte* hash);
00087
00088 private:
00089 void init (void);
00090 void transform (byte* data);
00091 void write (byte *inbuf, size_t inlen);
00092 void final (void);
00093 byte* read (void);
00094
00095 u32 A,B,C,D; /* chaining variables */
00096 u32 nblocks;
00097 byte buf[64];
00098 int count;
00099 };
00100
00101 #endif /* MD5_H */
00102
1.2.15