0

My C++ code creates a hash of a string using the standard functional library. I would like to port this function to Ruby, but as you can see, I lay my hashing into the strong, mighty hands of the C++ library.

Is there a direct equivalent in Ruby?

#include <functional>

int hashString(std::string_view str) {
    std::hash<std::string_view> hash_function;
    size_t h = hash_function(str);
    return 0x7FFFFFFFL & h;
}
Alex
  • 340
  • 2
  • 13
  • related/dupe: https://stackoverflow.com/questions/6536885/consistent-stringhash-based-only-on-the-strings-content – NathanOliver Apr 25 '23 at 14:28
  • it is not a generic "how to hash" question but actually "how to hash as C++ does" Here the standard library returns an int. The linked question returns a hex. Converting the int to hex in C++ does not yield the same result. – Alex Apr 25 '23 at 14:47
  • So you want your ruby code to produce the same value as the C++ code? – NathanOliver Apr 25 '23 at 14:48
  • The value of hashes should not matter as long as they are unique enough. But what's the point of only implementing the hash function in C++. Do you have a performance problem you can solve by doing this? – Pepijn Kramer Apr 25 '23 at 14:54
  • @NathanOliver exactly. I would like to produce the exact same values as in C++. CPP reference does not provide details on their hash function, thus it is a bit hard to know what to use in ruby. Also I am absolutely new to ruby – Alex Apr 25 '23 at 14:59
  • 2
    @Alex That is not going to be an easy task. First, C++ doesn't mandate how `std::hash` works, so different implementations may produce different values for the same string. Second, `std::hash` only has to produce the same hash value for a string in that execution of the program, which allows for having salted hashes, meaning each time you run the program you get a different hash value. If you want both the ruby code and the C++ code to produce the same value you'll need a library that works with both or roll your own. – NathanOliver Apr 25 '23 at 15:06
  • 5
    "I would like to produce the exact same values as in C++" – That is impossible, even in C++, since the C++ standard doesn't say what the values should be. It only says how the values should behave. So, even in C++, it is impossible to guarantee that you get the same value. – Jörg W Mittag Apr 25 '23 at 15:59
  • 4
    C++ gives only three guarantees: 1) if `a == b`, then `hash(a) == hash(b)`, 2) if `a != b`, then it is very unlikely that `hash(a) == hash(b)` (IOW, collisions are rare), 3) the value will not change during the runtime of the program. Which means, for example, it would be perfectly legal to return a different value every time you run the program. – Jörg W Mittag Apr 25 '23 at 16:07
  • Use a custom hash function in C++ and convert it to Ruby. From googling it looks like Ruby uses the typical C/C++ bitwise operators so should be easy to transliterate some string hash function you can find by googling. – jwezorek Apr 25 '23 at 16:23
  • 1
    I'm pretty sure modern hash implementations actually go out of their way to return different hashes for the same value on different runs to prevent collision attacks. For example, in Ruby 3.2 `1.hash` returns different values each time Ruby is spun up. – tadman Apr 25 '23 at 16:24
  • What is the objective here you're trying to achieve, and how does hashing factor into it? What precludes using a well-defined hash like SHA2 or SHA3? – tadman Apr 25 '23 at 16:25

0 Answers0