TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Crypto.h
Go to the documentation of this file.
1 
11 #ifndef G3D_Crypto_h
12 #define G3D_Crypto_h
13 
14 #include "G3D/platform.h"
15 #include "G3D/g3dmath.h"
16 #include "G3D/System.h"
17 #include <string>
18 
19 namespace G3D {
20 
22 class MD5Hash {
23 private:
24 
25  uint8 value[16];
26 
27 public:
28 
29  MD5Hash() {
30  for (int i = 0; i < 16; ++i) {
31  value[i] = 0;
32  }
33  }
34 
35  explicit MD5Hash(class BinaryInput& b);
36 
38  void rotateBytes() {
39  uint8 temp = value[0];
40  for (int i = 0; i < 15; ++i) {
41  value[i] = value[i + 1];
42  }
43  value[15] = temp;
44  }
45 
47  void rotateBytes(int n) {
48  uint8 temp[16];
49  System::memcpy(temp, value, 16);
50  for (int i = 0; i < 16; ++i) {
51  value[i] = value[(i + n) & 15];
52  }
53  }
54 
55  uint8& operator[](int i) {
56  return value[i];
57  }
58 
59  const uint8& operator[](int i) const {
60  return value[i];
61  }
62 
63  bool operator==(const MD5Hash& other) const {
64  bool match = true;
65  for (int i = 0; i < 16; ++i) {
66  match = match && (other.value[i] == value[i]);
67  }
68  return match;
69  }
70 
71  inline bool operator!=(const MD5Hash& other) const {
72  return !(*this == other);
73  }
74 
75  void deserialize(class BinaryInput& b);
76 
77  void serialize(class BinaryOutput& b) const;
78 
79  static size_t hashCode(const MD5Hash& key) {
80  size_t h = 0;
81  for (int i = 0; i < 4; ++i) {
82  const int x = i * 4;
83  h ^= (((uint32)key.value[x + 0]) << 24) |
84  (((uint32)key.value[x + 1]) << 16) |
85  (((uint32)key.value[x + 2]) << 8) |
86  ((uint32)key.value[x + 3]);
87  }
88  return h;
89  }
90 };
91 
92 
94 class Crypto {
95 public:
96 
105  static uint32 crc32(const void* bytes, size_t numBytes);
106 
113  static MD5Hash md5(const void* bytes, size_t numBytes);
114 
119  static int smallPrime(int n);
120 
122  static int numSmallPrimes();
123 };
124 
125 }
126 
127 #endif
static int smallPrime(int n)
Definition: Crypto.cpp:19
uint8 value[16]
Definition: Crypto.h:25
Definition: Crypto.h:22
static void memcpy(void *dst, const void *src, size_t numBytes)
Definition: System.cpp:643
Definition: BinaryInput.h:69
Definition: AABox.h:25
static MD5Hash md5(const void *bytes, size_t numBytes)
Definition: Crypto_md5.cpp:65
static size_t hashCode(const MD5Hash &key)
Definition: Crypto.h:79
static int numSmallPrimes()
Definition: Crypto.cpp:62
MD5Hash()
Definition: Crypto.h:29
void serialize(class BinaryOutput &b) const
Definition: Crypto_md5.cpp:31
void rotateBytes(int n)
Definition: Crypto.h:47
void rotateBytes()
Definition: Crypto.h:38
bool operator!=(const MD5Hash &other) const
Definition: Crypto.h:71
uint8_t uint8
Definition: g3dmath.h:164
static uint32 crc32(const void *bytes, size_t numBytes)
Definition: Crypto.cpp:66
Definition: BinaryOutput.h:52
void deserialize(class BinaryInput &b)
Definition: Crypto_md5.cpp:26
Definition: Crypto.h:94
uint32_t uint32
Definition: g3dmath.h:168
G3D::int16 x
Definition: Vector2int16.h:37
uint8 & operator[](int i)
Definition: Crypto.h:55
const uint8 & operator[](int i) const
Definition: Crypto.h:59
bool operator==(const MD5Hash &other) const
Definition: Crypto.h:63