EpcTools
An event based multi-threaded C++ development framework.
ehash.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2009-2019 Brian Waters
3 * Copyright (c) 2019 Sprint
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 
18 #ifndef __ehash_h_included
19 #define __ehash_h_included
20 
21 #include "ebase.h"
22 #include "estring.h"
23 #include "eerror.h"
24 
27 
29 class EHash
30 {
31 public:
35  static ULong getHash(EString &str);
40  static ULong getHash(cpChar val, ULong len);
45  static ULong getHash(cpUChar val, ULong len);
46 
47 private:
48  static ULong m_crcTable[256];
49 };
50 
53 
54 //
55 // ESipHash24 is adapted from the C reference implementation located at
56 // https://github.com/veorq/SipHash which is licensed to public domain by
57 // the [CC0 1.0 Universal] license.
58 //
59 
60 DECLARE_ERROR(ESipHash24Error_InvalidKeyLength);
61 
64 {
65 public:
67  class Key
68  {
69  public:
71  Key() : data_{0} {}
74  Key(const Key &k) { assign(k.data_,sizeof(k.data_)); }
78  Key(cpUChar k, const size_t len) { assign(k,len); }
79 
82  operator cpUChar() const { return data_; }
85  cpUChar data() const { return data_; }
86 
91  Key& assign(cpUChar k, const size_t len)
92  {
93  size_t i = 0;
94  for (; i<min(len,sizeof(data_)); i++)
95  data_[i] = k[i];
96  for( ; i < sizeof(data_); i++)
97  data_[i] = 0;
98  return *this;
99  }
100  private:
101  UChar data_[16];
102  };
103 
107  static Key& setKey(const Key& k) { return key_ = k; }
112  static Key& setKey(cpUChar val, const size_t len) { return key_.assign(val, len); }
115  static const Key& key() { return key_; }
116 
121  static ULong getHash32(EString &str, const Key &key=key_)
122  {
123  return getHash32(reinterpret_cast<cpUChar>(str.c_str()), str.length(), key);
124  }
130  static ULong getHash32(cpChar val, const size_t len, const Key &key=key_)
131  {
132  return getHash32(reinterpret_cast<cpUChar>(val), len, key);
133  }
139  static ULong getHash32(cpUChar in, const size_t inlen, const Key &key=key_);
147  static Bool getHash32(cpUChar in, const size_t inlen, pUChar out, const size_t outlen, const Key &key=key_);
148 
153  static ULongLong getHash64(EString &str, const Key &key=key_)
154  {
155  return getHash64(reinterpret_cast<cpUChar>(str.c_str()), str.length(), key);
156  }
162  static ULongLong getHash64(cpChar val, const size_t len, const Key &key=key_)
163  {
164  return getHash64(reinterpret_cast<cpUChar>(val), len, key);
165  }
171  static ULongLong getHash64(cpUChar in, const size_t inlen, const Key &key=key_);
179  static Bool getHash64(cpUChar in, size_t inlen, pUChar out, const size_t outlen, const Key &key=key_);
180 
187  static Bool getHash128(EString &str, pUChar out, const size_t outlen, const Key &key=key_)
188  {
189  return getHash128(reinterpret_cast<cpUChar>(str.c_str()), str.length(), out, outlen, key);
190  }
198  static Bool getHash128(cpChar val, size_t len, pUChar out, const size_t outlen, const Key &key=key_)
199  {
200  return getHash128(reinterpret_cast<cpUChar>(val), len, out, outlen, key);
201  }
209  static Bool getHash128(cpUChar in, size_t inlen, pUChar out, const size_t outlen, const Key &key=key_);
210 
211 private:
212  static Bool getFullHash(cpUChar in, size_t inlen, cpUChar k, pUChar out, const size_t outlen);
213  static Bool getHalfHash(cpUChar in, size_t inlen, cpUChar k, pUChar out, const size_t outlen);
214 
215  static Key key_;
216 };
217 
218 #endif // #define __ehash_h_included
ESipHash24::setKey
static Key & setKey(cpUChar val, const size_t len)
Assigns a new default key value.
Definition: ehash.h:112
eerror.h
Defines base class for exceptions and declaration helper macros.
ESipHash24::getHash32
static ULong getHash32(cpChar val, const size_t len, const Key &key=key_)
Returns the 32-bit hash associated with the character buffer.
Definition: ehash.h:130
ESipHash24::Key::assign
Key & assign(cpUChar k, const size_t len)
Assigns a new key value.
Definition: ehash.h:91
ebase.h
Macros for various standard C library functions and standard includes.
ESipHash24::setKey
static Key & setKey(const Key &k)
Assigns a new default key value.
Definition: ehash.h:107
ESipHash24::getHash128
static Bool getHash128(EString &str, pUChar out, const size_t outlen, const Key &key=key_)
Calculates the 128-bit hash associated with the string.
Definition: ehash.h:187
ESipHash24::Key::Key
Key(const Key &k)
Copy constructor.
Definition: ehash.h:74
ESipHash24::getHash64
static ULongLong getHash64(cpChar val, const size_t len, const Key &key=key_)
Returns the 64-bit hash associated with the character buffer.
Definition: ehash.h:162
ESipHash24::getHash64
static ULongLong getHash64(EString &str, const Key &key=key_)
Returns the 64-bit hash associated with the string.
Definition: ehash.h:153
EHash::getHash
static ULong getHash(EString &str)
Returns the 32-bit hash value for the specified string.
Definition: ehash.cpp:24
ESipHash24::getHash32
static ULong getHash32(EString &str, const Key &key=key_)
Returns the 32-bit hash associated with the string.
Definition: ehash.h:121
ESipHash24::key
static const Key & key()
Retrieves the default key.
Definition: ehash.h:115
ESipHash24::Key::Key
Key()
Class constructor.
Definition: ehash.h:71
ESipHash24::Key::Key
Key(cpUChar k, const size_t len)
Class constructor.
Definition: ehash.h:78
EString
String class.
Definition: estring.h:30
ESipHash24::Key
Represents a key to be used when calculating a hash value.
Definition: ehash.h:67
ESipHash24::Key::data
cpUChar data() const
Data extractor.
Definition: ehash.h:85
EHash
Calcuates a 32-bit hash value for the specified string or array of characters.
Definition: ehash.h:29
estring.h
Encapsulates and extends a std::string object.
DECLARE_ERROR
DECLARE_ERROR(ESipHash24Error_InvalidKeyLength)
ESipHash24
Calculates a 32-bit, 64-bit or 128-bit hash value using the SipHash algorithm.
Definition: ehash.h:63
ESipHash24::getHash128
static Bool getHash128(cpChar val, size_t len, pUChar out, const size_t outlen, const Key &key=key_)
Calculates the 128-bit hash associated with the character buffer.
Definition: ehash.h:198