Domanda

I'm new to hashing things and I'd like to be able to run a Hash through the MD5 hash to produce a unique value. At the moment I'm simply converting the Hash to a string and then just running that through the MD5 algorithm.

Is this a good way to do this or is there a better way to encrypt a Hash using MD5?

To clarify I'm trying to create a cache key for an object that doesn't have a unique id, so I'm trying to use the hash to create an id.

È stato utile?

Soluzione

Ruby hash built in support for Murmur3 hashing. If you are commited to md5 hashes then using this will not work. If you can use it, then just call the .hash method from the Object class ( http://ruby-doc.org/core-2.1.1/Object.html#method-i-hash ).

pry(main)> {}.hash
=> 0
pry(main)> {:a => "b"}.hash
=> 2051585302280555993
pry(main)> "foo".hash
=> -2886254516463342771

Murmur3 hashes are faster then md5, but may not be suitable for cryptographic uses. If you must use MD5 I see no reason why hashing a string is fundamentally bad. It may even ease to recreating the same hash in languages other than Ruby if required, because it separates the implementation from Ruby's internal binary format.

If you must use a cryptographic hash but are not committed to MD5, please use BCrypt, SHA1 or SHA256. MD5 is trivially reversible and not suitable for securing data.

edit: details and grammar

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top