Topic: short alphanumeric hash (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=31034" title="Pages that link to Topic: short alphanumeric hash (Page 1 of 1)" rel="nofollow" >Topic: short alphanumeric hash <span class="small">(Page 1 of 1)</span>\

 
GRUMBLE
Paranoid (IV) Mad Scientist

From: Omicron Persei 8
Insane since: Oct 2000

IP logged posted posted 06-06-2009 17:21 Edit Quote

hello, i am looking for a php function that generates a very short alphanumeric hash out of a string.

one that is similar to those tinyurls, example:
http://www.tinyurl.com/qmm52f

the md5 function which comes with php is way too long for me.

thanks!

Arthurio
Paranoid (IV) Inmate

From: cell 3736
Insane since: Jul 2003

IP logged posted posted 06-06-2009 17:40 Edit Quote

you can just take a part of the md5 if you want something quick ... but if you're doing something similar to tinyurl then the only way to ensure that all the hashes are unique would be to start with 0 and iterate then convert the number to base32 or 64

so what are you doing exactly and what are your requirements for this hash?

edit: didn't think of this before: http://php.net/manual/en/function.crc32.php

(Edited by Arthurio on 06-06-2009 17:49)

GRUMBLE
Paranoid (IV) Mad Scientist

From: Omicron Persei 8
Insane since: Oct 2000

IP logged posted posted 06-06-2009 17:49 Edit Quote

thanks!

the base32 encoding sounds interesting. is there a php function for that? thanks!

Arthurio
Paranoid (IV) Inmate

From: cell 3736
Insane since: Jul 2003

IP logged posted posted 06-06-2009 17:56 Edit Quote

try if echo base_convert(12345, 10, 32); works ... i'm in a hurry now, I can look into it later

poi
Paranoid (IV) Inmate

From: Norway
Insane since: Jun 2002

IP logged posted posted 06-06-2009 18:11 Edit Quote

Tinyurl and the likes don't actually hash the URL but increments an index and display in some sort of base #number_of_valid_URL_characters. IIRC there are 69 standards ASCII characters that are valid URL characters and don't require encoding. Therefore they simply do :

code:
function getHash( index )
{
  var valiCharacters = 'abcdefghi09832';
  var mod = valiCharacters.length;
  var hash = '';
  var tmp = index;
 
  while( tmp>mod )
  {
    hash+= validCharacters[ tmp%mod ];
    tmp = Math.flood( tmp/mod );
  }
  return hash;
}

Preceded by a lookup to see if the URL has already been remapped to a hash.

I tried crc32 before, but was worried by the possible collision. One thing I often do when I need a simple and relatively short UID without storing any sort of counter is use the current timestamp in base32.

HTH

GRUMBLE
Paranoid (IV) Mad Scientist

From: Omicron Persei 8
Insane since: Oct 2000

IP logged posted posted 06-06-2009 18:52 Edit Quote

yeah thanks for the infos! i went with base32 in the end.



Post Reply
 
Your User Name:
Your Password:
Login Options: Remember Me On This Computer
 
Your Text:
Loading...
Options: Show Signature
Enable Slimies
Enable Linkwords

« BackwardsOnwards »

Show Forum Drop Down Menu