tags:

views:

185

answers:

4

Hello,

I'm trying to find out how to build nice and short alpha numeric hashes like the kind used in youtube urls.

Example: http://www.youtube.com/watch?v=rw71YOSXhpE

Where rw71YOSXhpE would convert into video number 12834233 (for example).

These integers could be reversed in PHP to an integer and then looked up in a database.

I've run the following in PHP:

<?
$algoList = hash_algos( );

foreach( $algoList as $algoName )
{
    echo $algoName . ": " . hash( $algoName, 357892345234 ) . "\n";
}
?>

But none of them come back with characters beyond the a-f you'd expect. Youtube have the whole english alphabet in upper and lower case. Any idea how they've done it?

A: 

Something similar could be done with base64_encode().

Ignacio Vazquez-Abrams
But base64 includes characters like '='
Mark L
A: 

probably a base64 encoding of a (part of) an md5 ? although I seems to recall that there are short ones and long ones, so it could be md5 or sha1. if you base64 decode the token you gave, with proper padding, the result is an 8 bit entity, so it's not a full md5. It could be only the first half of it.

Stefano Borini
+3  A: 

You could use base_convert() to convert your number into base 36, which uses 0-9 plus a-z, and which has the advantage that your URL parameter is not case-sensitive.

Heinzi
+1  A: 

You want to convert your integer to a different base, one which uses the full alphabet. Base64 could work but you will get strings which are longer than the original integer because the base64_encode() function takes a string, not an integer.

My suggestion would be to use the base_convert() function like so:

$id = 12834233;
$hash = base_convert($id, 10, 36);

and the reverse

$hash = '7n2yh'
$id = base_convert($hash, 36, 10);

This however will only use lowercase letters a-z and 0-9. If you wish to use all upper and lower case letters you would need to convert to base 62 (or higher if you use symbols). However to do this you will have to write your own code.

Edit: Gordon pointed out this great link to base62 encoding in php.

bramp
Example for a base62 algorithm: http://programanddesign.com/php/base62-encode/
Gordon