PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
md5.h
Go to the documentation of this file.
1/* MD5
2 converted to C++ class by Frank Thilo (thilo@unix-ag.org)
3 for bzflag (http://www.bzflag.org)
4
5 based on:
6
7 md5.h and md5.c
8 reference implementation of RFC 1321
9
10 IMPLEMENTATION OBTAINED FROM http://www.zedwood.com/article/cpp-md5-function
11
12
13 Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
14rights reserved.
15
16License to copy and use this software is granted provided that it
17is identified as the "RSA Data Security, Inc. MD5 Message-Digest
18Algorithm" in all material mentioning or referencing this software
19or this function.
20
21License is also granted to make and use derivative works provided
22that such works are identified as "derived from the RSA Data
23Security, Inc. MD5 Message-Digest Algorithm" in all material
24mentioning or referencing the derived work.
25
26RSA Data Security, Inc. makes no representations concerning either
27the merchantability of this software or the suitability of this
28software for any particular purpose. It is provided "as is"
29without express or implied warranty of any kind.
30
31These notices must be retained in any copies of any part of this
32documentation and/or software.
33
34*/
35
36#ifndef BZF_MD5_H
37#define BZF_MD5_H
38
39#include <cstring>
40#include <iostream>
41
42
43// a small class for calculating MD5 hashes of strings or byte arrays
44// it is not meant to be fast or secure
45//
46// usage: 1) feed it blocks of uchars with update()
47// 2) finalize()
48// 3) get hexdigest() string
49// or
50// MD5(std::string).hexdigest()
51//
52// assumes that char is 8 bit and int is 32 bit
53class MD5
54{
55public:
56 typedef unsigned int size_type; // must be 32bit
57
58 MD5();
59 MD5(const std::string& text);
60 void update(const unsigned char *buf, size_type length);
61 void update(const char *buf, size_type length);
62 MD5& finalize();
63 std::string hexdigest() const;
64 friend std::ostream& operator<<(std::ostream&, MD5 md5);
65
66private:
67 void init();
68 typedef unsigned char uint1; // 8bit
69 typedef unsigned int uint4; // 32bit
70 enum {blocksize = 64}; // VC6 won't eat a const static int here
71
72 void transform(const uint1 block[blocksize]);
73 static void decode(uint4 output[], const uint1 input[], size_type len);
74 static void encode(uint1 output[], const uint4 input[], size_type len);
75
76 bool finalized;
77 uint1 buffer[blocksize]; // bytes that didn't fit in last 64 byte chunk
78 uint4 count[2]; // 64bit counter for number of bits (lo, hi)
79 uint4 state[4]; // digest so far
80 uint1 digest[16]; // the result
81
82 // low level logic operations
83 static inline uint4 F(uint4 x, uint4 y, uint4 z);
84 static inline uint4 G(uint4 x, uint4 y, uint4 z);
85 static inline uint4 H(uint4 x, uint4 y, uint4 z);
86 static inline uint4 I(uint4 x, uint4 y, uint4 z);
87 static inline uint4 rotate_left(uint4 x, int n);
88 static inline void FF(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
89 static inline void GG(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
90 static inline void HH(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
91 static inline void II(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
92};
93
94std::string md5(const std::string str);
95
96#endif
97
98
Definition md5.h:54
MD5 & finalize()
Definition md5.cpp:301
friend std::ostream & operator<<(std::ostream &, MD5 md5)
Definition md5.cpp:353
unsigned int size_type
Definition md5.h:56
std::string hexdigest() const
Definition md5.cpp:338
void update(const char *buf, size_type length)
void update(const unsigned char *buf, size_type length)
MD5()
Definition md5.cpp:105
std::string md5(const std::string str)
Definition md5.cpp:360