Please login or register. Welcome to the Studio, guest!


Quick Links:


newBookmarkLockedFalling

peter
Guest
I created this a few months ago, as I needed a way to create a sort of hash* to be passed to PHP from JavaScript.

This will create a 32 character long hash*. It's no md5 :P.

String.prototype.hash = function(){
     var iHashStr = "", iPreHash = "";
     var iCA = [];
     var iCount = 0;
     var iHexTab = "0123456789abcdef";

     if(this && this.length > 0){
          for(s = 0; s < (this.length * 8); s += 8){
               iCA |= ((this.charCodeAt(s / 8) & 255) << (s % 32));
          }

          iCA[(iCA.length * 8) >> 5] |= (128 << ((iCA.length * 8) % 32));

          while(iCA.length < 16){
               iCA[iCA.length] = Math.ceil(((iCA[iCount ++] * 2) + (iCount << 8)) >> 2);
          }

          iPreHash = iCA.toString().replace(/,/g, "").substring(0, 32);

          for(h = 0; h < 32; h ++){
               iHashStr += iHexTab.charAt((iPreHash.charAt(h) * 5) % 16);
          }
          return iHashStr;
     }
}


Now, to use it, just look at the example...


var myString = "javascript rocks";
var myStringHashed = myString.hash();
alert(myStringHashed)


Simple to use. the hashed* string is stored in the "myStringHashed" variable, and then alerted.




Here is the PHP version incase you want to compare the hash server side...

class crypt {
     var $iHexTab = "0123456789abcdef";
     var $iHashStr = "";

     function hash($str){
          $iPreHash = "";
          $iCount = 0;
          $iHashStr = "";
          $iCA = array();

          if($str && strlen($str) > 0){
               for($s = 0; $s < (strlen($str) * 8); $s += 8){
                    $iCA[$s >> 5] |= ((ord($str[$s / 8]) & 255) << ($s % 32));
               }

               $iCA[(count($iCA) * 8) >> 5] |= (128 << ((count($iCA) * 8) % 32));

               while(count($iCA) < 16){
                    $iCA[] = ceil((($iCA[$iCount ++] * 2) + ($iCount << 8)) >> 2);
               }

               foreach($iCA as $val){
                    $iPreHash .= $val;
               }

               $iPreHash = substr($iPreHash, 0, 32);
               $this -> iHashStr = "";

               for($h = 0; $h < 32; $h ++){
                    $this -> iHashStr .= ($this -> iHexTab[(intval($iPreHash[$h]) * 5) % 16]);
               }

               return $this -> iHashStr;
          }
     }
}


To use this one we need to create an instance first, so have a look at this example...

$digest = new crypt();
$hashed = $digest -> hash("PHP Rocks");
echo $hashed;


Hope it is useful for someone.


*a hash is not an encrypted version of the original inputted string, it is sort of like a finger print. So reversing the hash should be near impossible, but I can't say for sure that the generated hashes aren't reversible, so I wasn't sure if I should use the term "hash", but decided to in the end. Let me know if you can reverse it.


Last Edit: Nov 5, 2005 18:16:35 GMT by peter

newBookmarkLockedFalling