How to ensure that client created IDs aren't human-meaningful with a hash?

对着背影说爱祢 提交于 2019-12-25 07:19:12

问题


I want to allow untrused clients to create fairly short IDs, but enforce that they aren't human-meaningful (e.g. "l34fa75ljasd" is OK but "canada-is-evil" is not).

One approach to doing this would be to have the client create a value, hash it, and then use the hashed value as the ID (first suggested in this question).

I'm not sure what the best way to implement this is though.

The simplest approach I can think of would be for the client to create a random string, hash it with something like blake2b, base64 encode the result, and then truncate it down to the desired number of digits.

This approach seems to have problems though -- it might be easy to cause the first part of the hash (though not the whole thing) to collide with a desired string. Using the whole output of the hash as the ID would remove this problem, but it would conflict with our original goal of the results being fairly short. Also, I'm not sure how long the value that's provided as the hash function's input should be.

Could someone with more crypto expertise weigh in on the correct way to implement a system like this? My goal is for the resulting ID to be 128 base64 encoded bits, so 22 characters. I'd also like the results of the hashes to be spread evenly over that space so the IDs can be used as global identifiers without an increased chance of conflicts (if possible).

来源:https://stackoverflow.com/questions/38821364/how-to-ensure-that-client-created-ids-arent-human-meaningful-with-a-hash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!