Simple hand-made hash function

This example shows how to create a one-way encryption method by using a very simplified algorithm.

function SimpleHash($str){    
    $n = 0;
    // The magic happens here:
    // I just loop trough all letters and add the
    // ASCII value to a integer variable. 
    for ($c=0; $c < strlen($str); $c++)
        $n += ord($str[$c]);
    // After we went trough all letters
    // we have a number that represents the
    // content of the string
    return $n;
Snippet Details

$TestString = '';
print SimpleHash($TestString); 
// returns: 1620

Sorry folks, comments have been deactivated for now due to the large amount of spam.

Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!

Older comments:

SFX April 19, 2010 at 17:46
This isn't as save as a MD5 Hash, because I can see how much letters you have maximal used..

the highest possible value is 255


please excuse my bad English.. ;)
hiren March 23, 2010 at 05:16
Am giving you example wat i want...
$x = "fGuxnWJ5abcdef";
and in vb and java hshcode method i get this result after hashing in hex mode "74 C4 AB FB"
Now i want to get same hash result in php which i get from java and VB.
But i want 4 bytes in ascii.
Given result is in hex mode. Please help me out
hiren March 23, 2010 at 05:09
Hii bro.. I want to convert string in just 4 bytes. And method which i want to use is like java hashcode() and VB hash method. I want to get same output on php please tel me how to get same 4 bytes output on php which we get from VB and java hashcode() function.