hash
Hash
- 计算内存缓冲区的哈希
- 不推荐这个,改用FastHash
1 2 3 |
BASE_EXPORT uint32_t Hash(const void* data, size_t length); BASE_EXPORT uint32_t Hash(const std::string& str); BASE_EXPORT uint32_t Hash(const string16& str); |
1 2 3 4 5 6 7 8 9 10 11 |
uint32_t Hash(const void* data, size_t length) { return PersistentHash(data, length); } uint32_t Hash(const std::string& str) { return PersistentHash(str.data(), str.size()); } uint32_t Hash(const std::string16& str) { return PersistentHash(str.data(), str.size()*sizeof(char16)); } |
FastHash
- 真正快速高质的哈希
1 2 |
BASE_EXPORT size_t FastHash(base::span<const uint8_t> data); BASE_EXPORT size_t FastHash(const std::string& str); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
size_t FastHash(base::span<const uint8_t> data) { // We use the updated CityHash within our namespace (not the deprecated // version from third_party/smhasher). #if defined(ARCH_CPU_64_BITS) return base::internal::cityhash_v111::CityHash64( reinterpret_cast<const char*>(data.data()), data.size()); #else return base::internal::cityhash_v111::CityHash32( reinterpret_cast<const char*>(data.data()), data.size()); #endif } size_t FastHash(const std::string& str) { #if defined(ARCH_CPU_64_BITS) return base::internal::cityhash_v111::CityHash64(str.data(), str.size()); #else return base::internal::cityhash_v111::CityHash32(str.data(), str.size()); #endif } |
PersistentHash
- 计算内存缓冲区的哈希值
1 2 |
BASE_EXPORT uint32_t PersistentHash(const void* data, size_t length); BASE_EXPORT uint32_t PersistentHash(const std::string& str); |
1 2 3 4 5 6 7 8 9 10 11 12 |
uint32_t PersistentHash(const void* data, size_t length) { if (length > static_cast<size_t>(std::numric_limits<int>::max())) { NOTREACHED(); return 0; } return ::SuperFastHash(reinterpret_cast<const char*>(data), static_cast<int>(length)); } uint32_t PersistentHash(const std::string& str) { return PersistentHash(str.data(), str.size()); } |
HashInts32/HashInts64
- 32位或64位数字对的哈希
1 2 |
BASE_EXPORT size_t HashInts32(uint32_t value1, uint32_t value2); BASE_EXPORT size_t HashInts64(uint64_t value1, uint64_t value2); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
size_t HashInts32(uint32_t value1, uint32_t value2) { uint64_t value1_64 = value1; uint64_t hash64 = (value1_64 << 32) | value2; if (sizeof(size_t) >= sizeof(uint64_t)) return static_cast<size_t>(hash64); uint64_t odd_random = 481046412LL << 32 | 1025306955LL; uint32_t shift_random = 10121U << 16; hash64 = hash64 * odd_random + shift_random; size_t high_bits = static_cast<size_t>(hash64 >> (8 * (sizeof(uint64_t) - sizeof(size_t)))); return high_bits; } size_t HashInts64(uint64_t value1, uint64_t value2) { uint32_t short_random1 = 842304669U; uint32_t short_random2 = 619063811U; uint32_t short_random3 = 937041849U; uint32_t short_random4 = 3309708029U; uint32_t value1a = static_cast<uint32_t>(value1 & 0xffffffff); uint32_t value1b = static_cast<uint32_t>((value1 >> 32) & 0xffffffff); uint32_t value2a = static_cast<uint32_t>(value2 & 0xffffffff); uint32_t value2b = static_cast<uint32_t>((value2 >> 32) & 0xffffffff); uint64_t product1 = static_cast<uint64_t>(value1a) * short_random1; uint64_t product2 = static_cast<uint64_t>(value1b) * short_random2; uint64_t product3 = static_cast<uint64_t>(value2a) * short_random3; uint64_t product4 = static_cast<uint64_t>(value2b) * short_random4; uint64_t hash64 = product1 + product2 + product3 + product4; if (sizeof(size_t) >= sizeof(uint64_t)) return static_cast<size_t>(hash64); uint64_t odd_random = 1578233944LL << 32 | 194370989LL; uint32_t shift_random = 20591U << 16; hash64 = hash64 * odd_random + shift_random; size_t high_bits = static_cast<size_t>(hash64 >> (8 * (sizeof(uint64_t) - sizeof(size_t)))); return high_bits; } |
md5
MD5Init
- 初始化给定的MD5上下文结构,以供后续调用MD5Update()。
1 |
BASE_EXPORT void MD5Init(MD5Context* context); |
MD5Update
- 给定的作为StringPiece的data缓冲区,更新给定上下文与数据的MD5总和。
1 |
BASE_EXPORT void MD5Update(MD5Context* context, const StringPiece& data); |
MD5Final
- 完成MD5操作,并用摘要填充缓冲区。
1 |
BASE_EXPORT void MD5Final(MD5Digest* digest, MD5Context* context); |
MD5DigestToBase16
- 把一个MD5Digest转换成可读的十六进制字符串
1 |
BASE_EXPORT std::string MD5DigestToBase16(const MD5Digest& digest); |
MD5Sum
- 计算具有给定长度的给定数据缓冲区的MD5和。给定的“digest”结构将填充结果数据。
1 |
BASE_EXPORT void MD5Sum(const void* data, size_t length, MD5Digest* digest); |
MD5String
- 返回字符串的十六进制字符串
1 |
BASE_EXPORT std::string MD5String(const StringPiece& str); |
用法
1 2 3 4 5 6 |
std::string result = "test_aet"; base::MD5Context context; base::MD5Init(&context); base::MD5Update(&context, result); base::MD5Digest digest; base::MD5Final(&digest, &context); |
md5_constexpr
MD5Digest
- 计算提供数据的md5摘要
1 2 |
constexpr MD5Digest MD5SumConstexpr(const char* string); constexpr MD5Digest MD5SumConstexpr(const char* data, uint32_t length); |
MD5Hash64Constexpr/MD5Hash32Constexpr
- 计算提供数据的前32位64位的md5摘要
1 2 3 4 |
constexpr uint64_t MD5Hash64Constexpr(const char* string); constexpr uint64_t MD5Hash64Constexpr(const char* data, uint32_t length); constexpr uint32_t MD5Hash32Constexpr(const char* string); constexpr uint32_t MD5Hash32Constexpr(const char* data, uint32_t length); |
sha1
SHA1HashSpan
- 计算输入| data |的SHA-1哈希值 并返回完整的哈希。
1 2 |
BASE_EXPORT std::array<uint8_t, kSHA1Length> SHA1HashSpan( span<const uint8_t> data); |
SHA1HashString
- 计算输入字符串| str |的SHA-1哈希值 并返回全部哈希。
1 |
BASE_EXPORT std::string SHA1HashString(const std::string& str); |
SHA1HashBytes
- 计算| len |的SHA-1哈希 | data |中的字节 并把哈希在| hash |中。
|hash| 必须为kSHA1Length个字节长。
1 2 3 |
BASE_EXPORT void SHA1HashBytes(const unsigned char* data, size_t len, unsigned char* hash); |
本文为原创文章,版权归Aet所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ base_timer04/24
- ♥ Visual Studio:子进程调试相关07/15
- ♥ Cef:介绍06/29
- ♥ Base_file05/27
- ♥ base_json&&value05/19
- ♥ Chromium:鼠标事件的生成与处理07/19