Post by Peter on Aug 24, 2005 13:47:06 GMT
I have created this for Javascript and PHP, so if you need to send something that has been hashed* to PHP to do some comparing, then you can just call the digest PHP class.
Now, what it basically does is attempts to create a 32 character long hash*, sort of like how md5 and sha1 work, just not as good as them obviously
To see the Javascript version in action, have a play with this...
[CLICK]
Javascript version
<script type="text/javascript">
<!--
// pD-H Message Digest
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;
}
}
//-->
</script>
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.
PHP Version
Here is the same script but converted to PHP.
<?php
// pD-H Message Digest
class digest {
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 digest();
$hashed = $digest -> hash("PHP Rocks");
echo $hashed;
If ran, it will echo the hashed* string.
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.
Now, what it basically does is attempts to create a 32 character long hash*, sort of like how md5 and sha1 work, just not as good as them obviously
To see the Javascript version in action, have a play with this...
[CLICK]
Javascript version
<script type="text/javascript">
<!--
// pD-H Message Digest
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
}
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;
}
}
//-->
</script>
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.
PHP Version
Here is the same script but converted to PHP.
<?php
// pD-H Message Digest
class digest {
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 digest();
$hashed = $digest -> hash("PHP Rocks");
echo $hashed;
If ran, it will echo the hashed* string.
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.