[COLUG] Enterprise Two Factor Authentication

Jim jep200404 at columbus.rr.com
Tue Mar 13 16:50:25 EST 2007


Earlier I wrote:

>    if ($nBitsInJ>0)
>       $encodedS.=$encoding[$j & (1<<($nBitsInJ))];

The more I think about it, 

   if ($nBitsInJ>0)
      $encodedS.=$encoding[($j<<($nBitsPerEncodedChar-$nBitsInJ)) & (1<<($nBitsPerEncodedChar))];

would be better for its consistent bit placement. 
Perhaps even better would be the following for using the same code from the loop, 
to yield: 

   if ($nBitsInJ>0) { /* pad left over bits with zeros */
      $j      <<=($nBitsPerEncodedChar-$nBitsInJ);
      $nBitsInJ+=($nBitsPerEncodedChar-$nBitsInJ);
   }

   for ( ;$nBitsInJ>=$nBitsPerEncodedChar;$nBitsInJ-=$nBitsPerEncodedChar)
      $encodedS.=$encoding[($j>>($nBitsInJ-$nBitsPerEncodedChar))
      & (1<<($nBitsPerEncodedChar)];

which begs for a big #define macro! :-)

   #define HolyMacro {\
      for ( ;$nBitsInJ>=$nBitsPerEncodedChar;$nBitsInJ-=$nBitsPerEncodedChar)\
         $encodedS.=$encoding[($j>>($nBitsInJ-$nBitsPerEncodedChar))\
         & (1<<($nBitsPerEncodedChar)];\
   }

   function encode($s,$nBitsPerEncodedChar,$encoding)
   {
...
      for ($i=0;$i<strlen($s);$i++) {
...
         HolyMacro
      }

      if ($nBitsInJ>0) { /* pad left over bits with zeros */
         $j      <<=($nBitsPerEncodedChar-$nBitsInJ);
         $nBitsInJ+=($nBitsPerEncodedChar-$nBitsInJ);
      }

      HolyMacro

I leave it for the student to port that macro to PHP and come up with 
a better name for it. BTW, I notice a typo in my earlier code. 

    $encodedS.=$encoding[($j>>($nBitsInJ-nBitsPerEncodedChar))

should have been

    $encodedS.=$encoding[($j>>($nBitsInJ-$nBitsPerEncodedChar))

Jim



More information about the colug432 mailing list