PHP function to create 8 hash hashes ([az] = no numbers allowed)

I need a PHP function that will generate 8 hash characters [az] from any input string. So, for example, when I send "Stack Overflow", it will return, for example. "gdqreaxc" (8 characters [az] numbers not allowed)

+7
php hash
source share
5 answers

Maybe something like:

$hash = substr(strtolower(preg_replace('/[0-9_\/]+/','',base64_encode(sha1($input)))),0,8); 

This creates a SHA1 hash, base-64 encodes it (gives us the full alphabet), removes non-alpha characters, reduces it and truncates it.

For $input = 'yar!'; :

mwinzewn

For $input = 'yar!!'; :

yzzhzwjj

So the spread seems pretty good.

+9
source share

This function generates a hash containing evenly spaced [az] characters:

 function my_hash($string, $length = 8) { // Convert to a string which may contain only characters [0-9a-p] $hash = base_convert(md5($string), 16, 26); // Get part of the string $hash = substr($hash, -$length); // In rare cases it will be too short, add zeroes $hash = str_pad($hash, $length, '0', STR_PAD_LEFT); // Convert character set from [0-9a-p] to [az] $hash = strtr($hash, '0123456789', 'qrstuvwxyz'); return $hash; } 
By the way, if this is important for you, for 100,000 different lines you will have a ~ 2% chance of a hash collision (for an 8-hour hash), and for a million lines this chance rises to ~ 90% if my math is correct.
+2
source share
 function md5toabc($myMD5) { $newString = ""; for ($i = 0; $i < 16; $i+=2) { //add the first val of 0-15 to the second val of 0-15 for a range of 0-30 $myintval = hexdec(substr($myMD5, $i, $i +1) ) + hexdec(substr($myMD5, $i+1, $i +2) ); // mod by 26 and add 97 to get to the lowercase ascii range $newString .= chr(($myintval%26) + 97); } return $newString; } 

Note that this introduces bias for different characters, but do what you want with it. (For example, when you roll two dice, the most common is 7 combined ...) plus a module, etc.

0
source share

you can give a good ap {8} (but not az), using and modifying (output) the well-known algorithm:

 function mini_hash( $string ) { $h = hash( 'crc32' , $string ); for($i=0;$i<8;$i++) { $h{$i} = chr(96+hexdec($h{$i})); } return $h; } 

an interesting set of restrictions that you posted there

0
source share

What about

 substr (preg_replace(md5($mystring), "/[1-9]/", ""), 0, 8 ); 

you could add a little more entorpy by doing

 preg_replace($myString, "1", "g"); preg_replace($myString, "2", "h"); preg_replace($myString, "3", "i"); 

etc. instead of deleting numbers.

-one
source share

All Articles