哈希函数生成 30 个字符的代码?

发布于 2024-09-15 21:15:38 字数 50 浏览 6 评论 0原文

我需要将消息散列成 30 个字符的字符串。对于这种用途,最好、最安全的哈希函数是什么?

I need to hash a message into a string of 30 chars. What's the best and most secure hash function for this usage?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

—━☆沉默づ 2024-09-22 21:15:38

30 个字符(字节)相当于 240 位。

如果您无法将球门柱移动到允许 32 个字符,那么您最终可能会使用 SHA-1,它会生成 160 位或 20 个字节。当 Base-64 编码时,这将是 28 个字符。如果您使用十六进制编码,则它将是 40 个字符,这名义上超出了范围。对于 32 个字符,您可以使用 SHA-256,但 Base-64 编码会增加该大小(至 44 个字符),而十六进制编码会将大小增加至 64 个字符。

如果必须使用十六进制编码并且可以达到 32 字节,则可以使用 MD5(生成 128 位),但不建议在任何新系统中使用。对于 Base-64 编码,MD5 使用 24 个字符。否则,您将使用非常不安全的算法 - 根本不推荐。

Thirty characters (bytes) is 240 bits.

If you can't move the goal-post to allow 32 characters, then you will probably end up using SHA-1, which generates 160-bits or 20 bytes. When Base-64 encoded, that will be 28 characters. If you use a hex-encoding, it will be 40 characters, which is nominally out of range. With 32 characters, you could use SHA-256, but Base-64 encoding would increase that size (to 44 characters) and hex-encoding increases the size to 64 characters.

If you must use hex-encoding and can go to 32 bytes, then MD5 - which generates 128 bits - could be used, though it is not recommended for any new systems. With Base-64 encoding, MD5 uses 24 characters. Otherwise, you are using very minimally secure algorithms - not recommended at all.

自演自醉 2024-09-22 21:15:38

只需使用 SHA1 并修剪至 30 个字符。

import hashlib
hash = hashlib.sha1("your message").hexdigest()[:30]

事实证明,从加密安全哈希函数(例如 SHA1)中删除字符对其安全性的影响可以忽略不计(但现在找不到参考)

Just use SHA1 and trim to 30 characters.

import hashlib
hash = hashlib.sha1("your message").hexdigest()[:30]

It's been proven that cutting characters off a cryptographically secure hash function such as SHA1 has negligible effects on its security (can't find the reference now though)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文