Linux 是否有 glibc 哈希函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3936117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a glibc hash function?
提问by themaestro
I'm looking to do a custom hash table implementation in C. Is there an MD5/SHA1 hash function already in the GNU library or do I have to use an external library for this?
我希望在 C 中实现自定义哈希表。 GNU 库中是否已有 MD5/SHA1 哈希函数,或者我是否必须为此使用外部库?
Here's kinda what I'm looking for:
这是我正在寻找的东西:
int hashValue;
hashValue = MD5_HASH(valToHash);
采纳答案by Michael Burr
You can take a look at Bob Jenkin's survey and analysis of many hash functions:
你可以看看 Bob Jenkin 对很多哈希函数的调查和分析:
Or just drop his lookup3 routines (which he's put into the public domain) into your project:
或者只是将他的 lookup3 例程(他已放入公共领域)放入您的项目中:
回答by Dirk Eddelbuettel
There are a few trusted, simple versions available -- I have a few in the sources of the digestfor R. Here is what I wrote in the DESCRIPTION file:
有几个值得信赖的,简单的可用版本-我在的来源几消化的[R 。这是我在描述文件中写的:
Description: The digest package provides functions for the creation of `hash' digests of arbitrary R objects using the md5, sha-1, sha-256 and crc32 algorithms permitting easy comparison of R language objects. The md5 algorithm by Ron Rivest is specified in RFC 1321, the SHA-1 and SHA-256 algorithms are specified in FIPS-180-1 and FIPS-180-2, and the crc32 algorithm is described in
ftp://ftp.rocksoft.com/cliens/rocksoft/papers/crc_v3.txt. For md5, sha-1 and sha-256, this packages uses small standalone implementations that were provided by Christophe Devine. For crc32, code from the zlib library is used.
描述:摘要包提供了使用 md5、sha-1、sha-256 和 crc32 算法创建任意 R 对象的“散列”摘要的函数,允许轻松比较 R 语言对象。Ron Rivest 的 md5 算法在 RFC 1321 中指定,SHA-1 和 SHA-256 算法在 FIPS-180-1 和 FIPS-180-2 中指定,crc32 算法在
ftp://ftp.rocksoft 中描述.com/cliens/rocksoft/papers/crc_v3.txt。对于 md5、sha-1 和 sha-256,这个包使用由 Christophe Devine 提供的小型独立实现。对于 crc32,使用 zlib 库中的代码。
I think some of Christophe's code is no longer at cr0.net, but searches should lead you to several other projects incorporating it. His file headers were pretty clear:
我认为 Christophe 的一些代码不再在 cr0.net 上,但是搜索应该会引导您找到其他几个包含它的项目。他的文件头很清楚:
/*
* FIPS-180-1 compliant SHA-1 implementation,
* by Christophe Devine <[email protected]>;
* this program is licensed under the GPL.
*/
and his code matches the reference output.
并且他的代码与参考输出相匹配。
回答by Hank Gay
Unless you already have a good reason for using MD5, you may want to reconsider. What makes for a "good" hash function in a hash table is pretty dependent on what you're trying to accomplish. You may want to read the comments in Python's dictobject.c
to see the sorts of tradeoffs others have made.
除非您已经有充分的理由使用 MD5,否则您可能需要重新考虑。散列表中“好的”散列函数的构成非常依赖于您要完成的任务。您可能希望阅读 Python 中的注释dictobject.c
以了解其他人所做的各种权衡。
回答by ninjalj
Glibc's crypt()
uses a MD5 based algorhytm if salt starts with $1$. But since you mention that you are going to do a hash table implementation, maybe Jenkins hash would be more appropiate.
crypt()
如果 salt 以 $1$ 开头,则Glibc使用基于 MD5 的算法。但是既然你提到你要做一个哈希表实现,也许 Jenkins 哈希会更合适。
回答by Chris
The OpenSSL library has all the crypto routines you could ever want, including cryptographic hashes.
OpenSSL 库拥有您可能想要的所有加密例程,包括加密哈希。
回答by Thomas Pornin
For a hash table, you do not need cryptographic strength, only good randomization properties. Broken cryptographic hash functions (like MD5) are fine for that, but you may want to use MD4, which is both faster and simpler, to the point that you could simply include an implementation directly in your code. It is not difficult to rewrite it from the specification (and since you want only a function for a hash table, it is not really a problem if you get it wrong at some point). Shameless plug: there is an optimized C implementation of MD4 in sphlib.
对于哈希表,您不需要加密强度,只需要良好的随机化属性。损坏的加密哈希函数(如 MD5)对此很好,但您可能希望使用MD4,它既更快又更简单,以至于您可以直接在代码中包含一个实现。从规范中重写它并不困难(并且由于您只需要一个哈希表的函数,因此如果您在某些时候弄错了,这并不是真正的问题)。无耻的插件:sphlib 中有 MD4 的优化 C 实现。
回答by karl
gcrypt and openssl can do MD5, SHA and other hashes here's an example with libgcrypt:
gcrypt 和 openssl 可以执行 MD5、SHA 和其他哈希,这里是 libgcrypt 的一个例子:
#include <gcrypt.h>
#include <stdio.h>
// compile gcc md5_test.c -lgcrypt
int main(int argc, char *argv[])
{
unsigned char digest[16];
char digest_ascii[32+1] = {0,};
int digest_length = gcry_md_get_algo_dlen (GCRY_MD_MD5);
int i;
printf("hashing=%s len=%d\n", argv[1], digest_length);
gcry_md_hash_buffer(GCRY_MD_MD5, digest, argv[1], strlen(argv[1]));
for (i=0; i < digest_length; i++) {
sprintf(digest_ascii+(i*2), "%02x", digest[i]);
}
printf("hash=%s\n", digest_ascii);
}
`
`
回答by UnixShadow
Murmur3 is a fast noncryptographic algorithm that you can use.
Murmur3 是一种可以使用的快速非加密算法。
A good speed comparation of murmur against other algorithms can be found in this thread https://softwareengineering.stackexchange.com/questions/49550/which-hashing-algorithm-is-best-for-uniqueness-and-speed
可以在此线程中找到杂音与其他算法的良好速度比较https://softwareengineering.stackexchange.com/questions/49550/which-hashing-algorithm-is-best-for-uniqueness-and-speed
One possible implementation: https://github.com/PeterScott/murmur3
一种可能的实现:https: //github.com/PeterScott/murmur3
Example:
例子:
uint32_t hash;
uint32_t seed = 42;
char* input = "HelloWorld";
MurmurHash3_x86_32(input, strlen(input), seed, &hash);
printf("x86_32: %08x\n", hash);