首页 > 解决方案 > 根据较长的输入字符串生成恰好 10 个字符的随机且唯一的密钥

问题描述

我需要根据某些特定字段为每条记录生成恰好 10 个字符的随机且唯一的密钥。如果我下次提供相同的信息集,它应该给我相同的密钥。

总之,我正在寻找一种将更长的字符串始终如一地转换为 10 个字符的字符串的方法。

类似于 md5 哈希的东西,但应该只输出 10 个字符。

谢谢。

注意:作为评论之一问我尝试了什么。基本上我还没有找到任何合适的解决方案并对其进行了大量研究。我能想到的唯一解决方案是将 md5 哈希和 10 个字符的密钥存储到一个数据库中,以便下次查找。可以生成一个自定义的 10 个字符的键,它与 md5 散列没有任何关系,除了映射 db。

标签: javahashmd5

解决方案


What you're asking to do is not possible. The question title says, "generate random and unique key of exactly 10 characters based on longer input string".

By the Pigeonhole principle, you can't do that. Because there are more strings that are longer than 10 characters than there are strings that are exactly 10 characters long, any hashing function you come up with will generate duplicates. Multiple long strings will map to the same 10-character string.

You can't guarantee uniqueness.


推荐阅读