moved back to old acc
[vox.git] / src / core / hash.hpp
blobdb23259f42802749a01247c42a87cf3cd175c514
2 #ifndef __standalone_hashpp_header_defined__
3 #define __standalone_hashpp_header_defined__
5 #include <exception>
6 #include <stdexcept>
7 #include <string>
8 #include <vector>
10 namespace Hash
13 class Error: public std::runtime_error
15 public:
16 Error(const std::string& msg): std::runtime_error(msg)
20 virtual ~Error() throw()
26 /**
27 * The HashObject "superclass".
28 * For maximum portability, rely -only- on functions provided in HashObject.
30 class Object
32 public:
33 typedef unsigned int size_type;
35 public:
36 virtual ~Object()
39 virtual void update(const std::string& input) = 0;
40 virtual void update(const char* input, size_type length) = 0;
41 virtual void update(char input) = 0;
42 virtual void updateFromFile(const std::string& path);
43 virtual void finalize() = 0;
44 virtual std::string hexDigest() = 0;
49 class MD5: public Object
51 public: // types
52 typedef unsigned char uint1; // 8bit
53 typedef unsigned int uint4; // 32bit
55 public: // static
56 static std::string GenMD5(const std::string& str, unsigned int length=0);
58 private: // enums
59 enum
61 blocksize = 64
64 private: // variables
65 bool finalized;
66 bool cleanedup;
67 char* digest_buffer;
68 MD5::uint1* buffer;
69 MD5::uint4* count;
70 MD5::uint4* state;
71 MD5::uint1* digest;
73 private:
74 void presetup();
76 void init();
78 // apply MD5 algo on a block
79 void transform(const uint1 block[blocksize]);
81 // decodes input (unsigned char) into output (uint4).
82 // Assumes len is a multiple of 4.
83 void decode(uint4 output[], const uint1 input[], size_type len);
86 // encodes input (uint4) into output (unsigned char). Assumes len is
87 // a multiple of 4.
88 void encode(uint1 output[], const uint4 input[], size_type len);
90 // low level logic operations
91 uint4 logic_F(uint4 x, uint4 y, uint4 z);
93 uint4 logic_G(uint4 x, uint4 y, uint4 z);
95 uint4 logic_H(uint4 x, uint4 y, uint4 z);
97 uint4 logic_I(uint4 x, uint4 y, uint4 z);
99 uint4 rotate_left(uint4 x, int n);
101 void rot_round1(uint4 &a, uint4 b, uint4 c, uint4 d,
102 uint4 x, uint4 s, uint4 ac);
104 void rot_round2(uint4 &a, uint4 b, uint4 c, uint4 d,
105 uint4 x, uint4 s, uint4 ac);
107 void rot_round3(uint4 &a, uint4 b, uint4 c, uint4 d,
108 uint4 x, uint4 s, uint4 ac);
110 void rot_round4(uint4 &a, uint4 b, uint4 c, uint4 d,
111 uint4 x, uint4 s, uint4 ac);
113 public: // functions
114 MD5();
115 virtual ~MD5();
116 void update(const std::string& input);
117 void update(const unsigned char* input, size_type length);
118 void update(const char* input, size_type length);
119 void update(char input);
120 void finalize();
121 void cleanup();
122 std::string hexDigest();
125 class SHA1: public Object
127 public:
128 static std::string GenSHA1(const std::string& data, unsigned int length=0);
130 private: // vars
131 // Message digest buffers
132 std::vector<unsigned int> H;
134 // 512-bit message blocks
135 unsigned int* Message_Block;
137 // words block
138 unsigned int* Words;
140 // Message length in bits
141 unsigned int Length_Low;
143 // Message length in bits
144 unsigned int Length_High;
146 // Index into message block array
147 int Message_Block_Index;
149 // Is the digest computed?
150 bool Computed;
152 // Is the message digest corruped?
153 bool Corrupted;
155 // is the digest finalized?
156 bool finalized;
158 private:
159 void ProcessMessageBlock();
160 void PadMessage();
161 unsigned int CircularShift(int bits, unsigned int word);
163 public:
164 SHA1();
165 virtual ~SHA1();
166 void reset();
167 void update(const std::string& data);
168 void update(const char* message_array, unsigned int length);
169 void update(char message_element);
170 bool result(int* message_digest_array);
171 void finalize();
172 std::string hexDigest();
175 } // namespace Hash
177 #endif /* __standalone_hashpp_header_defined__ */