Create short hash in PHP

This has been talked about many times here about SO. But I did not find a solution to my problem.

I want to create a short hash (for example, max 8 characters) for the prompt system. I can’t use base[X] encodingbecause it would be too easy to guess. I can't just trim extra characters, for example. a MD5hash because I think the collision problem will come up after a while.

Is there a solution for this?

+5
source share
3 answers

If you want to be sure that you have never encountered a conflict, it is best to maintain a database of valid hashes and compare it with this database when creating new hashes.

, , , " " , . , .

+2

substr SHA1 MD5. substr'd , , .

, , - :

define('KEY_CHARS', 'acefghjkpqrstwxyz23456789'); // characters which cannot be confused phonetically or by bad handwriting

function generateKey($len = 8) {
    $k = str_repeat('.', $len);
    while ($len--) {
        $k[$len] = substr(KEY_CHARS, mt_rand(0, strlen(KEY_CHARS) - 1), 1);
    }
    return $k;
}
0

- md5. Md5 16 = 128- . base 64, 6 / char.

md5 22 ( , b64).

. / + , os.

Base64 (by replacing / and +) ensures that your hash does not spoil URLs with special characters.

0
source

All Articles