PHP Classes

class_SHA1Library.php patch

Recommend this page to a friend!

      Jabber Client  >  All threads  >  class_SHA1Library.php patch  >  (Un) Subscribe thread alerts  
Subject:class_SHA1Library.php patch
Summary:Fixes for using the b64_hmac_sha1 function
Messages:1
Author:Marc Davignon
Date:2008-09-26 17:37:46
 

  1. class_SHA1Library.php patch   Reply   Report abuse  
Picture of Marc Davignon Marc Davignon - 2008-09-26 17:37:46
In using this function I came across a couple bugs which produced incorrect output.

In particular there are some sizeof() used when they should be strlen() and a couple other variables are missing the $.

See the patch below. There are several other changes made but most are for my own needs and are not necessary changes:

--- /home/mpdavig/proj-nosync/downloads/jabberclass-0.9/class_SHA1Library.php 2007-08-04 20:15:09.000000000 -0400
+++ lib/class_SHA1Library.php 2008-09-26 12:09:21.000000000 -0400
@@ -1,4 +1,4 @@
-<?
+<?php
/*
* This file was contributed (in part or whole) by a third party, and is
* released under a BSD-compatible free software license. Please see the
@@ -32,7 +32,7 @@
* the server-side, but the defaults work in most cases.
*/
var $hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
- var $b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
+ var $b64pad = "="; /* base-64 pad character. "=" for strict RFC compliance */
var $chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */

/*
@@ -63,7 +63,7 @@
$x[$len >> 5] |= 0x80 << (24 - $len % 32);
$x[(($len + 64 >> 9) << 4) + 15] = $len;

- $w = Array();
+ $w = array();
$a = 1732584193;
$b = -271733879;
$c = -1732584194;
@@ -101,7 +101,7 @@
$e = $this->safe_add($e, $olde);
}

- return Array($a, $b, $c, $d, $e);
+ return array($a, $b, $c, $d, $e);
}

/*
@@ -154,10 +154,10 @@
function core_hmac_sha1($key, $data)
{
$bkey = $this->str2binb($key);
- if(sizeof($bkey) > 16) $bkey = $this->core_sha1($bkey, sizeof($key) * $this->chrsz);
+ if(sizeof($bkey) > 16) $bkey = $this->core_sha1($bkey, strlen($key) * $this->chrsz);

- $ipad = Array();
- $opad = Array();
+ $ipad = array();
+ $opad = array();

for($i = 0; $i < 16; $i++)
{
@@ -165,7 +165,7 @@
$opad[$i] = $bkey[$i] ^ 0x5C5C5C5C;
}

- $hash = $this->core_sha1(array_merge($ipad,$this->str2binb($data)), 512 + sizeof($data) * $this->chrsz);
+ $hash = $this->core_sha1(array_merge($ipad,$this->str2binb($data)), 512 + strlen($data) * $this->chrsz);
return $this->core_sha1(array_merge($opad,$hash), 512 + 160);
}

@@ -194,7 +194,7 @@
*/
function str2binb($str)
{
- $bin = Array();
+ $bin = array();
$mask = (1 << $this->chrsz) - 1;
for($i = 0; $i < strlen($str) * $this->chrsz; $i += $this->chrsz)
$bin[$i >> 5] |= (ord($str{$i / $this->chrsz}) & $mask) << (24 - $i%32);
@@ -237,7 +237,7 @@
{
$tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
$str = "";
- for($i = 0; i < sizeof($binarray) * 4; $i += 3)
+ for($i = 0; $i < sizeof($binarray) * 4; $i += 3)
{
$triplet = ((($binarray[$i >> 2] >> 8 * (3 - $i %4)) & 0xFF) << 16)
| ((($binarray[$i+1 >> 2] >> 8 * (3 - ($i+1)%4)) & 0xFF) << 8 )
@@ -245,7 +245,7 @@
for($j = 0; $j < 4; $j++)
{
if($i * 8 + $j * 6 > sizeof($binarray) * 32) $str .= $this->b64pad;
- else $str .= $tab{($triplet >> 6*(3-j)) & 0x3F};
+ else $str .= $tab{($triplet >> 6*(3-$j)) & 0x3F};
}
}
return $str;