hash: extract fnv1a hashes to new unit
This commit is contained in:
parent
588b38fd17
commit
ff23146a68
@ -53,7 +53,6 @@ UTIL_FILES = \
|
||||
guid.cpp \
|
||||
guid.hpp \
|
||||
hash.hpp \
|
||||
hash.cpp \
|
||||
hash/adler.cpp \
|
||||
hash/adler.hpp \
|
||||
hash/bsdsum.cpp \
|
||||
@ -62,6 +61,8 @@ UTIL_FILES = \
|
||||
hash/crc.hpp \
|
||||
hash/fasthash.cpp \
|
||||
hash/fletcher.hpp \
|
||||
hash/fnv1a.cpp \
|
||||
hash/fnv1a.hpp \
|
||||
hash/hmac.cpp \
|
||||
hash/hmac.hpp \
|
||||
hash/hotp.cpp \
|
||||
|
59
hash.cpp
59
hash.cpp
@ -1,59 +0,0 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2010 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#include "hash.hpp"
|
||||
|
||||
|
||||
// 32: 2^ 24 + 2^8 + 0x93
|
||||
// 64: 2^ 40 + 2^8 + 0xb3
|
||||
// 128: 2^ 88 + 2^8 + 0x3B
|
||||
// 256: 2^168 + 2^8 + 0x63
|
||||
// 512: 2^344 + 2^8 + 0x57
|
||||
// 1024: 2^680 + 2^8 + 0x8D
|
||||
static const uint32_t FNV_1a_32_PRIME = 16777619U;
|
||||
static const uint64_t FNV_1a_64_PRIME = 1099511628211U;
|
||||
|
||||
// Bias is the FNV-0 hash of "chongo <Landon Curt Noll> /\\../\\"
|
||||
static const uint32_t FNV_1a_32_BIAS = 2166136261U;
|
||||
static const uint64_t FNV_1a_64_BIAS = 14695981039346656037U;
|
||||
|
||||
|
||||
uint32_t
|
||||
util::fnv1a32 (const void *_data, size_t size) {
|
||||
const uint8_t *data = static_cast<const uint8_t*> (_data);
|
||||
uint32_t result = FNV_1a_32_BIAS;
|
||||
|
||||
for (size_t i = 0; i < size; ++i) {
|
||||
result ^= data[i];
|
||||
result *= FNV_1a_32_PRIME;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
uint64_t
|
||||
util::fnv1a64 (const void *_data, size_t size) {
|
||||
const uint8_t *data = static_cast<const uint8_t*> (_data);
|
||||
uint64_t result = FNV_1a_64_BIAS;
|
||||
|
||||
for (size_t i = 0; i < size; ++i) {
|
||||
result ^= data[i];
|
||||
result *= FNV_1a_64_PRIME;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
29
hash.hpp
29
hash.hpp
@ -11,35 +11,20 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2010 Danny Robson <danny@nerdcruft.net>
|
||||
* Copyright 2010-2015, Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_HASH_HPP
|
||||
#define __UTIL_HASH_HPP
|
||||
|
||||
#include "hash/wang.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
|
||||
// All hashes are unsuitable for cryptographic operations nnless noted.
|
||||
namespace util {
|
||||
// Fast and general hashing using FNV-1a
|
||||
uint32_t fnv1a32 (const void *, size_t);
|
||||
uint64_t fnv1a64 (const void *, size_t);
|
||||
|
||||
// General hashes for when you really just don't care about implementation
|
||||
//inline uint32_t hash (uint32_t key) { return wang32 (key); }
|
||||
//inline int32_t hash ( int32_t key) { return (int32_t) hash ((uint32_t)key); }
|
||||
|
||||
//inline uint64_t hash (uint64_t key) { return wang64 (key); }
|
||||
//inline int64_t hash ( int64_t key) { return (int64_t) hash ((uint64_t)key); }
|
||||
|
||||
//inline uintptr_t hash (const void *key) {
|
||||
// return sizeof (uintptr_t) == 32 ? wang32 (reinterpret_cast<uintptr_t> (key)) :
|
||||
// wang64 (reinterpret_cast<uintptr_t> (key));
|
||||
//}
|
||||
}
|
||||
|
||||
|
||||
|
||||
namespace util { namespace hash {
|
||||
uint32_t mix (uint32_t v) { return wang (key); }
|
||||
uint64_t mix (uint64_t v) { return wang (key); }
|
||||
} }
|
||||
|
||||
#endif
|
||||
|
75
hash/fnv1a.cpp
Normal file
75
hash/fnv1a.cpp
Normal file
@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2010-2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#include "fnv1a.hpp"
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Prime is,
|
||||
// 32: 2^ 24 + 2^8 + 0x93
|
||||
// 64: 2^ 40 + 2^8 + 0xb3
|
||||
// 128: 2^ 88 + 2^8 + 0x3B
|
||||
// 256: 2^168 + 2^8 + 0x63
|
||||
// 512: 2^344 + 2^8 + 0x57
|
||||
// 1024: 2^680 + 2^8 + 0x8D
|
||||
//
|
||||
// Bias is the FNV-0 hash of "chongo <Landon Curt Noll> /\\../\\"
|
||||
|
||||
template <size_t B>
|
||||
struct constants { };
|
||||
|
||||
template <> struct constants<32> {
|
||||
static constexpr uint32_t PRIME = 16777619u;
|
||||
static constexpr uint32_t BIAS = 2166136261u;
|
||||
};
|
||||
|
||||
template <> struct constants<64> {
|
||||
static constexpr uint64_t PRIME = 1099511628211u;
|
||||
static constexpr uint64_t BIAS = 14695981039346656037u;
|
||||
};
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
T
|
||||
fnv1a (const void *restrict _data, size_t len)
|
||||
{
|
||||
auto *data = static_cast<const uint8_t *restrict> (_data);
|
||||
T result = constants<sizeof(T)*8>::BIAS;
|
||||
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
result ^= data[i];
|
||||
result *= constants<sizeof(T)*8>::PRIME;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
uint32_t
|
||||
util::hash::fnv1a32 (const void *data, size_t len)
|
||||
{
|
||||
return fnv1a<uint32_t> (data, len);
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
uint64_t
|
||||
util::hash::fnv1a64 (const void *data, size_t len)
|
||||
{
|
||||
return fnv1a<uint64_t> (data, len);
|
||||
}
|
29
hash/fnv1a.hpp
Normal file
29
hash/fnv1a.hpp
Normal file
@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Copyright 2010-2015 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_HASH_FNV1A_HPP
|
||||
#define __UTIL_HASH_FNV1A_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
|
||||
namespace util { namespace hash {
|
||||
// Fast and general hashing using FNV-1a
|
||||
uint32_t fnv1a32 (const void *restrict, size_t);
|
||||
uint64_t fnv1a64 (const void *restrict, size_t);
|
||||
} }
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user