Youtube URL style hashes

I am trying to figure out how to create beautiful and short alphanumeric hashes, such as the types used in youtube URLs.

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

Where rw71YOSXhpE will convert to video number 12834233 (for example).

These integers could be undone in PHP to an integer, and then searched in the database.

In PHP, I ran the following:

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

But none of them come back with the characters you expect. Youtube has the entire English alphabet in upper and lower case. Any idea how they did it?

+4
source share
5 answers

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

+5
source

You want to convert an integer to another base that uses the full alphabet. Base64 may work, but you get strings that 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 as follows:

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

and vice versa

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

This, however, will only use the lowercase letters az and 0-9. If you want to use all upper and lower case letters, you will need to convert to base 62 (or higher if you use characters). However, for this you will have to write your own code.

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

+6
source

I had a similar problem, and I wrote for myself just for that.

Documentation: http://www.hashids.org/php/

Souce: https://github.com/ivanakimov/hashids.php

You would use it as follows:

 require('lib/Hashids/Hashids.php'); $hashids = new Hashids\Hashids('salt value', 11); $hash = $hashids->encrypt(12834233); 

You will receive the following $hash : Rz0zlKZGg6g

Provide your own unique string for the salt value. Code 11 is optional and indicates the minimum hash length. (You can also define your own alphabet string as the third parameter for the constructor).

To decrypt a hash, you must do this:

 $numbers = $hashids->decrypt($hash); 

So $numbers will be: [12834233]

(This is an array because hashids can encrypt / decrypt multiple numbers into a single hash.)

EDIT

  • Changed URLs to include both the document website and the source code
  • Changed sample code for configuring major lib updates (current version of PHP lib 0.3.0 - thanks to the whole open source community for improving lib)
+5
source

Something similar could be done with base64_encode() .

-1
source

perhaps base64 encoding (part) of md5? although I seem to remember that there are short and long, so it could be md5 or sha1. if you base64 decode the token you gave with the correct fill, the result will be an 8-bit object, so it is not full md5. It can only be the first half.

-1
source

Source: https://habr.com/ru/post/1300864/


All Articles