简体   繁体   English

python <=> c ++跨语言哈希

[英]python <=> c++ cross-language hash

I am writing a python program to look up a file. 我正在编写一个python程序来查找文件。 The file was created by a C++ program with a hash as filename ( std::hash<std::string> hash_fn ). 该文件由C ++程序创建,其哈希为文件名( std::hash<std::string> hash_fn )。 I know the string from which the hash was created, but I can't find a python hash function that produces the same hash (I tried hash() and all in hashlib ). 我知道创建哈希的字符串,但我找不到生成相同哈希的python哈希函数(我尝试了hash()hashlib所有hashlib )。 As an example, the string 作为一个例子,字符串

file:///home/ubuntu/Untitled.skp

should give the hash: 应该给哈希:

3133433022

Unfortunately I don't have control over the C++ program, only about the python script (or eventual python extensions). 不幸的是,我无法控制C ++程序,只关于python脚本(或最终的python扩展)。 Is it possible to find or implement the same hash function that C++ uses? 是否有可能找到或实现C ++使用的相同哈希函数? Or should I try another approach? 或者我应该尝试另一种方法?

For the most part, internal hash functions like std::hash in C++ or the Python hash function are not designed for external use. 在大多数情况下,内部散列函数(如C ++中的std::hash或Python散列函数不是为外部使用而设计的。 When you're designing such systems, strictly specify the hash function used, and implement it in both systems. 在设计此类系统时,请严格指定所使用的哈希函数,并在两个系统中实现它。

If it's too late for this, and you've already used std::hash , then about all you can do is find the sources for it (which depending on the compiler, may not be available), back engineer them to find the hashing algorithm used, specify it as your hash, and reimplement it in whatever languages needed. 如果现在为时已晚,并且您已经使用了std::hash ,那么您所能做的就是找到它的源代码(取决于编译器,可能不可用),重新设计它们以找到散列使用的算法,将其指定为您的哈希值,并以所需的任何语言重新实现它。 (You need to implement it in your own code, because it could potentially change in the next release of your compiler.) (您需要在自己的代码中实现它,因为它可能会在下一版本的编译器中发生变化。)

To be compatible, use a known hash on both sides, like SHA-1. 要兼容,请在SHA-1两侧使用已知的哈希值。 Python has it builtin, and if c++ doesn't have it built in, there are many libraries that do. Python内置了它,如果c ++没有内置,那么有很多库可以做到。

If you have control over the C++ part, you can make sure you use the same hashing algorithm on both sides. 如果您可以控制C ++部分,则可以确保在两侧使用相同的散列算法。 Alternatively, you can always have a look at the implementation of the hash_fn and try to re-implement the same functionality in Python. 或者,您可以随时查看hash_fn的实现,并尝试在Python中重新实现相同的功能。

Otherwise it may be quite hard to try to match the hashing functionality. 否则,尝试匹配散列功能可能会非常困难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM