I want when user sign up the app automatic generate a unique name like user-53s5ww or user-789kss. How can I do this in Java?
Asked
Active
Viewed 163 times
2 Answers
0
One simple way is that you can use system timestamp, construct the name as "user-"+System.currentTimeMillis()
The name will be like "user-780832399474455" and will be unique

Hoàn Lê
- 127
- 1
- 4
-
it's good but too long – SuPeR StAr Feb 09 '20 at 15:14
-
Ideally Id should be a time based unique random string. You can reduce time offset from EPOCH time to something else to get smaller time value. You can also look into String shortening algorithms. Please review - https://stackoverflow.com/questions/6299574/shortening-a-string-in-java – Vijayendra Feb 09 '20 at 17:42
0
You can use user + (The length of User table in your DB + 1) so If you have 40 users in your DB when a new user signed up it can have the name like user-41. You have to execute one query to save the user anyway so it has also not extra call to DB.

emredmrcn
- 144
- 1
- 5
-
@EmreDemircan You are technically right, but this solutions exposes pattern of user ids to attackers. Let me try to explain this with example. Suppose if an attacker finds a security bug on your site and he knows his user ID is ID-40, then he just need to run a loop based on this simple pattern to attack other user's data. Ideally id string should be a random string. In case if random strings, an attacker may still attack with a random string user id but it will produce more failed cases on server and analyzing logs of failures will give you hint of system under attack. – Vijayendra Feb 09 '20 at 17:36