Topic awaiting preservation: short alphanumeric hash |
|
---|---|
Author | Thread |
Paranoid (IV) Mad Scientist From: Omicron Persei 8 |
posted 06-06-2009 17:21
hello, i am looking for a php function that generates a very short alphanumeric hash out of a string. |
Paranoid (IV) Inmate From: cell 3736 |
posted 06-06-2009 17:40
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 |
Paranoid (IV) Mad Scientist From: Omicron Persei 8 |
posted 06-06-2009 17:49 |
Paranoid (IV) Inmate From: cell 3736 |
posted 06-06-2009 17:56
try if echo base_convert(12345, 10, 32); works ... i'm in a hurry now, I can look into it later |
Paranoid (IV) Inmate From: Norway |
posted 06-06-2009 18:11
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. |
Paranoid (IV) Mad Scientist From: Omicron Persei 8 |
posted 06-06-2009 18:52 |