一串散列是特定长度的长度、散列是

2023-09-11 04:43:07 作者:眉雪

有没有一种方法来生成字符串的哈希值,使散列本身是特定长度的?我有一个生成41字节的哈希值(SHA-1),但我需要它是33个字节的最大(因为某些硬件的限制)。如果我截断41个字节的哈希值到33,我可能会(当然!)失去了独特性。

Is there a way to generate a hash of a string so that the hash itself would be of specific length? I've got a function that generates 41-byte hashes (SHA-1), but I need it to be 33-bytes max (because of certain hardware limitations). If I truncate the 41-byte hash to 33, I'd probably (certainly!) lost the uniqueness.

或者实际上我想的MD5算法将很好地适应,如果我能找到一些C code一个与你的帮助。

Or actually I suppose an MD5 algorithm would fit nicely, if I could find some C code for one with your help.

在此先感谢任何提示/指点!

Thanks in advance for any tips/pointers!

编辑:谢谢大家的快速和知识渊博的反应。我选择去与MD5哈希,它适合罚款我的目的。唯一性是一个重要的问题,但我不希望这些哈希的数量是非常大的,在任何给定的时间 - 这些哈希值重新present软件的服务器上的一个家庭局域网,所以在最高也就5,也许10运行。

Thank you all for the quick and knowledgeable responses. I've chosen to go with an MD5 hash and it fits fine for my purpose. The uniqueness is an important issue, but I don't expect the number of those hashes to be very large at any given time - these hashes represent software servers on a home LAN, so at max there would be 5, maybe 10 running.

推荐答案

的单向散列计算这是不幸的是没有可能的。为了限制哈希长度33字节,你将不得不削减它。你可以异或第一和最后33字节,因为这可能保留更多的信息。但即使有33个字节,你没有碰撞的大的机会。

The way hashes are calculated that's unfortunately not possible. To limit the hash length to 33 bytes, you will have to cut it. You could xor the first and last 33 bytes, as that might keep more of the information. But even with 33 bytes you don't have that big a chance of a collision.

MD5: http://www.md5hashing.com/c++/

BTW。 MD5为16字节,SHA1 20个字节,SHA256是32个字节,但是因为hexstrings,他们都在规模翻番。如果你可以存储字节,你甚至可以使用SHA256。

btw. md5 is 16 bytes, sha1 20 bytes and sha256 is 32 bytes, however as hexstrings, they all double in size. If you can store bytes, you can even use sha256.