java - 根据较长的输入字符串生成恰好 10 个字符的随机且唯一的密钥
问题描述
我需要根据某些特定字段为每条记录生成恰好 10 个字符的随机且唯一的密钥。如果我下次提供相同的信息集,它应该给我相同的密钥。
总之,我正在寻找一种将更长的字符串始终如一地转换为 10 个字符的字符串的方法。
类似于 md5 哈希的东西,但应该只输出 10 个字符。
谢谢。
注意:作为评论之一问我尝试了什么。基本上我还没有找到任何合适的解决方案并对其进行了大量研究。我能想到的唯一解决方案是将 md5 哈希和 10 个字符的密钥存储到一个数据库中,以便下次查找。可以生成一个自定义的 10 个字符的键,它与 md5 散列没有任何关系,除了映射 db。
解决方案
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.
推荐阅读
- javascript - 通过 redux 更改状态后如何更新 React 组件?
- excel - 使用 XMLHTTP 方法时等到页面加载完毕
- ruby-on-rails - Rails 如何在项目级别访问用户状态
- vuejs2 - 如何在 vue-bootstrap-datetimepicker 中使用我自己的图标
- android - 文本颜色更改未反映在列表视图中
- c# - 基于实时值进行或退出单元测试的正确方法
- time-series - 从时间序列数据中删除电子伪影
- java - 如何在java中减去两次不同的日期
- google-apps-script - 将所有工作表中的所有文本自动更改为大写,但不是整个 Google 电子表格中的数字
- javascript - 捕获按钮的值