Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get 32-bit hash value from boost::hash

Tags:

c++

hash

boost

I am using boost::hash to get hash value for a string. But it is giving different hash values for same string on Windows 32-bit and Debian 64-bit systems.

So how can I get same hash value (32-bit or 64-bit) using boost::hash irrespective of platform?

like image 387
user2099350 Avatar asked Jul 02 '13 13:07

user2099350


People also ask

Can we get the original value from hash?

The cryptographic hashing of a value cannot be inverted to find the original value. Given a value, it is infeasible to find another value with the same cryptographic hash.

What is a 32 bit hash?

The 32-bit long hash value is a hexadecimal number of 8 characters. MD4 is a Message Digest Algorithm developed by Ronald L. Rivest from RSA Data Security, Inc. Currently it's considered insecure, but it's very fast on 32-bit mashines and it's used for calculating EDonkey 2000 hashes in the EDonkey p2p network.

How do you value hash?

Hashing involves applying a hashing algorithm to a data item, known as the hashing key, to create a hash value. Hashing algorithms take a large range of values (such as all possible strings or all possible files) and map them onto a smaller set of values (such as a 128 bit number). Hashing has two main applications.

What is boost Hash_combine?

x); boost::hash_combine (seed, p. y); return seed; } ... }; Calls to hash_combine incrementally build the hash from the different members of point, it can be repeatedly called for any number of elements. It calls hash_value on the supplied element, and combines it with the seed.


1 Answers

What is the guarantee concerning boost::hash? I don't see any guarantees that a generated hash code is usable outside of the process which generates it. (This is frequently the case with hash functions.) If you need a hash value for external data, valid over different programs and different platforms (e.g. for a hashed access to data on disk), then you'll have to write your own. Something like:

uint32_t
hash( std::string const& key )
{
    uint32_t results = 12345;
    for ( auto current = key.begin(); current != key.end(); ++ current ) {
        results = 127 * results + static_cast<unsigned char>( *current );
    }
    return results;

}

should do the trick, as long as you don't have to worry about porting to some exotic mainframes (which might not support uint32_t).

like image 101
James Kanze Avatar answered Sep 26 '22 07:09

James Kanze