最多20个字符的字母数字输出哈希算法

编程入门 行业动态 更新时间:2024-10-26 06:28:59
本文介绍了最多20个字符的字母数字输出哈希算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要一个散列算法,输出一个最长为20个字符的字母数字字符串。对于字母数字,我的意思是 [a-zA-Z0-9] 。

输入是 UUID s的规范形式(例如 550e8400-e29b -41d4-a716-446655440000 )

另一种方法是将SHA1或MD5散列转换为具有这些限制的字符串? / b>

谢谢。

编辑

不需要加密安全。碰撞使数据不准确,但如果它们偶尔发生,我可以忍受它。

编辑2

我不知道截断MD5或SHA1是否会使碰撞频繁发生。现在我想知道是否最好将20个字符截断为MD5值或SHA1值。 解决方案

你不需要从GUID的散列。使用良好的散列函数,散列的任何部分的不可预测性都与该部分的大小成正比。如果你愿意,你可以编码它的基地32,而不是标准的十六进制基地16.记住,这不会显着改善每个字符的熵(只有25%)。

<对于非加密用途,截断MD5,SHA1或SHA2无关紧要。在熵方面也没有明显的不足。

I need an hash algorithm that outputs an alphanumeric string that is max 20 characters long. For "alphanumeric" I mean [a-zA-Z0-9].

Inputs are UUIDs in canonical form (example 550e8400-e29b-41d4-a716-446655440000)

In alternative is there a way to convert a SHA1 or MD5 hash to a string with these limitations?

Thanks.

EDIT

Doesn't need to be cryptographically secure. Collisions make data inaccurate, but if they happen sporadically I can live with it.

EDIT 2

I don't know if truncating MD5 or SHA1 would make collisions happen too often. Now I'm wondering if it's better to truncate to 20 chars a MD5 value or a SHA1 value.

解决方案

Just clip the characters you don't need from the hash of the GUID. With a good hash function, the unpredictability of any part of the hash is proportional to the part's size. If you want, you can encode it base 32 instead of the standard hex base 16. Bear in mind that this will not significantly improve entropy per character (only by 25%).

For non-cryptographic uses, it does not matter whether you truncate MD5, SHA1 or SHA2. Neither has any glaring deficiencies in entropy.

更多推荐

最多20个字符的字母数字输出哈希算法

本文发布于:2023-11-30 10:00:23,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最多   算法   字母   字符   数字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!