1 /*
2  * HLLib
3  * Copyright (C) 2006-2013 Ryan Gregg
4 
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your Option) any later
9  * version.
10  */
11 
12 #ifndef CHECKSUM_H
13 #define CHECKSUM_H
14 
15 #include "stdafx.h"
16 #include "Error.h"
17 
18 namespace HLLib
19 {
20 	hlULong Adler32(const hlByte *lpBuffer, hlUInt uiBufferSize, hlULong uiAdler32 = 0);
21 	hlULong CRC32(const hlByte *lpBuffer, hlUInt uiBufferSize, hlULong uiCRC = 0);
22 
23 	struct MD5Context
24 	{
25 		hlULong lpState[4];
26 		hlULong lpBlock[16];
27 		hlULong uiLength;
28 	};
29 
30 	hlVoid MD5_Initialize(MD5Context& context);
31 	hlVoid MD5_Update(MD5Context& context, const hlByte *lpBuffer, hlUInt uiBufferSize);
32 	hlVoid MD5_Finalize(MD5Context& context, hlByte (&lpDigest)[16]);
33 
34 	struct SHA1Context
35 	{
36 		hlULong lpState[5];
37 		hlULong lpBlock[16];
38 		hlULong uiLength;
39 	};
40 
41 	hlVoid SHA1_Initialize(SHA1Context& context);
42 	hlVoid SHA1_Update(SHA1Context& context, const hlByte *lpBuffer, hlUInt uiBufferSize);
43 	hlVoid SHA1_Finalize(SHA1Context& context, hlByte (&lpDigest)[20]);
44 
45 	class Checksum
46 	{
47 	public:
~Checksum()48 		virtual ~Checksum()
49 		{
50 		}
51 
52 		virtual hlULong GetDigestSize() const = 0;
53 		virtual void Initialize() = 0;
54 		virtual void Update(const hlByte *lpBuffer, hlUInt uiBufferSize) = 0;
55 		virtual bool Finalize(const hlByte *lpHash) = 0;
56 	};
57 
58 	class CRC32Checksum : public Checksum
59 	{
60 	public:
CRC32Checksum()61 		CRC32Checksum()
62 		{
63 			Initialize();
64 		}
65 
GetDigestSize()66 		virtual hlULong GetDigestSize() const
67 		{
68 			return sizeof(this->uiChecksum);
69 		}
70 
Initialize()71 		virtual void Initialize()
72 		{
73 			this->uiChecksum = 0;
74 		}
75 
Update(const hlByte * lpBuffer,hlUInt uiBufferSize)76 		virtual void Update(const hlByte *lpBuffer, hlUInt uiBufferSize)
77 		{
78 			this->uiChecksum = CRC32(lpBuffer, uiBufferSize, this->uiChecksum);
79 		}
80 
Finalize(const hlByte * lpHash)81 		virtual bool Finalize(const hlByte *lpHash)
82 		{
83 			return *reinterpret_cast<const hlULong*>(lpHash) == this->uiChecksum;
84 		}
85 
86 	private:
87 		hlULong uiChecksum;
88 	};
89 
90 	class MD5Checksum : public Checksum
91 	{
92 	public:
MD5Checksum()93 		MD5Checksum()
94 		{
95 			Initialize();
96 		}
97 
GetDigestSize()98 		virtual hlULong GetDigestSize() const
99 		{
100 			return sizeof(this->context.lpState);
101 		}
102 
Initialize()103 		virtual void Initialize()
104 		{
105 			MD5_Initialize(this->context);
106 		}
107 
Update(const hlByte * lpBuffer,hlUInt uiBufferSize)108 		virtual void Update(const hlByte *lpBuffer, hlUInt uiBufferSize)
109 		{
110 			MD5_Update(this->context, lpBuffer, uiBufferSize);
111 		}
112 
Finalize(const hlByte * lpHash)113 		virtual bool Finalize(const hlByte *lpHash)
114 		{
115 			hlByte lpDigest[16];
116 			MD5_Finalize(this->context, lpDigest);
117 			return memcmp(lpHash, lpDigest, sizeof(lpDigest)) == 0;
118 		}
119 
120 	private:
121 		MD5Context context;
122 	};
123 
124 	class SHA1Checksum : public Checksum
125 	{
126 	public:
SHA1Checksum()127 		SHA1Checksum()
128 		{
129 			Initialize();
130 		}
131 
GetDigestSize()132 		virtual hlULong GetDigestSize() const
133 		{
134 			return sizeof(this->context.lpState);
135 		}
136 
Initialize()137 		virtual void Initialize()
138 		{
139 			SHA1_Initialize(this->context);
140 		}
141 
Update(const hlByte * lpBuffer,hlUInt uiBufferSize)142 		virtual void Update(const hlByte *lpBuffer, hlUInt uiBufferSize)
143 		{
144 			SHA1_Update(this->context, lpBuffer, uiBufferSize);
145 		}
146 
Finalize(const hlByte * lpHash)147 		virtual bool Finalize(const hlByte *lpHash)
148 		{
149 			hlByte lpDigest[20];
150 			SHA1_Finalize(this->context, lpDigest);
151 			return memcmp(lpHash, lpDigest, sizeof(lpDigest)) == 0;
152 		}
153 
154 	private:
155 		SHA1Context context;
156 	};
157 }
158 
159 #endif
160