How to create a reliable hash?

How to create a 8-character hash to be sure this can be unique in several million cases? I don’t want it for password, for example how bitly is generating this kind of hash for url and it is unique in millions of urls?
Any php coding suggestion?

CSPRNG

https://www.php.net/manual/en/function.random-bytes.php

That makes no sense, as any hash depends on the input value, so you have to define WHICH million cases you mean. By random i may get a collision just within two tries. And hashes are never unique, as they are just a reduction of the input, so you get an infinite amount of collisions when exeeding the hash size.

Pretty sure OP is just misusing the word hash. He is asking about a Unique Id.

Yes, unique id, thanks for random_bytes, but what if we have certain value and we want to create a unique id base on the value?

For example how stripe is generating a unique id for a CC data? May be it is storing cc data in db and map it with a unique id and when it uses this id to search cc data? Or what?

What is the point? A unique id is a unique id no matter how it was generated.

Try searching the online PHP manual for the time () and microtime() functions, both return numerics unlikely ever to repeat :slight_smile:

The same topic i want to know into better way. Is there any one to help me?

What’s wrong with the answers that have already been given???

I guess it depends on how you define “several” in the first post. A simple incrementing number will give you up to 99 million unique IDs.

if you specify what you want it for then we might give you better answers

I suppose they are simply generating a random string and uniqueness can be checked by a unique key in the database.