哈希函数生成 30 个字符的代码?
我需要将消息散列成 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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.
只需使用 SHA1 并修剪至 30 个字符。
事实证明,从加密安全哈希函数(例如 SHA1)中删除字符对其安全性的影响可以忽略不计(但现在找不到参考)
Just use SHA1 and trim to 30 characters.
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)