I have a system where each account of a user has a unique id, I can’t user that id as a referral link because it contains dashes
I need to generate a unique referral code given that id, I thought about using a
sha256 of that id.
The problem is that the
sha256 is too long to be used as a referral but if I truncate it, the collision chances increase.
Is there any way to generate a referral link given an id?
this is the format id:
If your user IDs are random and unique, then you can’t use them to generate shorter IDs without collisions (because you’re effectively trying to compress random data).
Realistically though, are collisions a concern? If you have 500 users a a truncated has has a one in 100 billion chance of colliding, is that really a problem? Depends how many users you have, and how long your codes can be.
But referral links don’t need to be secret, they just need to be unique. So at the simplest level you could just generate sequential integers and use those for the IDs? Sure, you could guess other valid referral IDs, but who cares?
If you want them to be a bit more complex, you could generate random strings, and check them against the current list of IDs in the database to make sure that they’re unique. But this might slow down once you have millions of users.