Source for file base32.php

Documentation is available at base32.php

  1. <?php
  2. /**
  3.  * @package    FrameworkOnFramework
  4.  * @subpackage encrypt
  5.  * @copyright  Copyright (C) 2010 - 2012 Akeeba Ltd. All rights reserved.
  6.  * @license    GNU General Public License version 2 or later; see LICENSE.txt
  7.  */
  8. defined('_JEXEC'or die;
  9.  
  10. /**
  11.  * FOFEncryptBase32
  12.  *
  13.  * @package  FrameworkOnFramework
  14.  * @since    1.0
  15.  */
  16. {
  17.     /**
  18.      * CSRFC3548
  19.      *
  20.      * The character set as defined by RFC3548
  21.      * @link http://www.ietf.org/rfc/rfc3548.txt
  22.      */
  23.     const CSRFC3548 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
  24.  
  25.     /**
  26.      * str2bin
  27.      *
  28.      * Converts any ascii string to a binary string
  29.      *
  30.      * @param   string  $str  The string you want to convert
  31.      *
  32.      * @return  string  String of 0's and 1's
  33.      */
  34.     private function str2bin($str)
  35.     {
  36.         $chrs unpack('C*'$str);
  37.  
  38.         return vsprintf(str_repeat('%08b'count($chrs))$chrs);
  39.     }
  40.  
  41.     /**
  42.      * bin2str
  43.      *
  44.      * Converts a binary string to an ascii string
  45.      *
  46.      * @param   string  $str  The string of 0's and 1's you want to convert
  47.      *
  48.      * @return  string  The ascii output
  49.      *
  50.      * @throws Exception
  51.      */
  52.     private function bin2str($str)
  53.     {
  54.         if (strlen($str0)
  55.         {
  56.             throw new Exception('Length must be divisible by 8');
  57.         }
  58.  
  59.         if (!preg_match('/^[01]+$/'$str))
  60.         {
  61.             throw new Exception('Only 0\'s and 1\'s are permitted');
  62.         }
  63.  
  64.         preg_match_all('/.{8}/'$str$chrs);
  65.         $chrs array_map('bindec'$chrs[0]);
  66.  
  67.         // I'm just being slack here
  68.         array_unshift($chrs'C*');
  69.  
  70.         return call_user_func_array('pack'$chrs);
  71.     }
  72.  
  73.     /**
  74.      * fromBin
  75.      *
  76.      * Converts a correct binary string to base32
  77.      *
  78.      * @param   string  $str  The string of 0's and 1's you want to convert
  79.      *
  80.      * @return  string  String encoded as base32
  81.      *
  82.      * @throws exception
  83.      */
  84.     private function fromBin($str)
  85.     {
  86.         if (strlen($str0)
  87.         {
  88.             throw new Exception('Length must be divisible by 8');
  89.         }
  90.  
  91.         if (!preg_match('/^[01]+$/'$str))
  92.         {
  93.             throw new Exception('Only 0\'s and 1\'s are permitted');
  94.         }
  95.  
  96.         // Base32 works on the first 5 bits of a byte, so we insert blanks to pad it out
  97.         $str preg_replace('/(.{5})/''000$1'$str);
  98.  
  99.         // We need a string divisible by 5
  100.         $length strlen($str);
  101.         $rbits $length 7;
  102.  
  103.         if ($rbits 0)
  104.         {
  105.             // Excessive bits need to be padded
  106.             $ebits substr($str$length $rbits);
  107.             $str substr($str0$length $rbits);
  108.             $str .= "000$ebitsstr_repeat('0'strlen($ebits));
  109.         }
  110.  
  111.         preg_match_all('/.{8}/'$str$chrs);
  112.         $chrs array_map(array($this'_mapcharset')$chrs[0]);
  113.  
  114.         return join(''$chrs);
  115.     }
  116.  
  117.     /**
  118.      * toBin
  119.      *
  120.      * Accepts a base32 string and returns an ascii binary string
  121.      *
  122.      * @param   string  $str  The base32 string to convert
  123.      *
  124.      * @return  string  Ascii binary string
  125.      *
  126.      * @throws  Exception
  127.      */
  128.     private function toBin($str)
  129.     {
  130.         if (!preg_match('/^[' self::CSRFC3548 ']+$/'$str))
  131.         {
  132.             throw new Exception('Must match character set');
  133.         }
  134.  
  135.         // Convert the base32 string back to a binary string
  136.         $str join(''array_map(array($this'_mapbin')str_split($str)));
  137.  
  138.         // Remove the extra 0's we added
  139.         $str preg_replace('/000(.{5})/''$1'$str);
  140.  
  141.         // Unpad if nessicary
  142.         $length strlen($str);
  143.         $rbits $length 7;
  144.  
  145.         if ($rbits 0)
  146.         {
  147.             $str substr($str0$length $rbits);
  148.         }
  149.  
  150.         return $str;
  151.     }
  152.  
  153.     /**
  154.      * fromString
  155.      *
  156.      * Convert any string to a base32 string
  157.      * This should be binary safe...
  158.      *
  159.      * @param   string  $str  The string to convert
  160.      *
  161.      * @return  string  The converted base32 string
  162.      */
  163.     public function encode($str)
  164.     {
  165.         return $this->fromBin($this->str2bin($str));
  166.     }
  167.  
  168.     /**
  169.      * toString
  170.      *
  171.      * Convert any base32 string to a normal sctring
  172.      * This should be binary safe...
  173.      *
  174.      * @param   string  $str  The base32 string to convert
  175.      *
  176.      * @return  string  The normal string
  177.      */
  178.     public function decode($str)
  179.     {
  180.         $str strtoupper($str);
  181.  
  182.         return $this->bin2str($this->tobin($str));
  183.     }
  184.  
  185.     /**
  186.      * _mapcharset
  187.      *
  188.      * Used with array_map to map the bits from a binary string
  189.      * directly into a base32 character set
  190.      *
  191.      * @param   string  $str  The string of 0's and 1's you want to convert
  192.      *
  193.      * @return  string  Resulting base32 character
  194.      *
  195.      * @access private
  196.      */
  197.     private function _mapcharset($str)
  198.     {
  199.         // Huh!
  200.         $x self::CSRFC3548;
  201.  
  202.         return $x[bindec($str)];
  203.     }
  204.  
  205.     /**
  206.      * _mapbin
  207.      *
  208.      * Used with array_map to map the characters from a base32
  209.      * character set directly into a binary string
  210.      *
  211.      * @param   string  $chr  The caracter to map
  212.      *
  213.      * @return  string  String of 0's and 1's
  214.      *
  215.      * @access private
  216.      */
  217.     private function _mapbin($chr)
  218.     {
  219.         return sprintf('%08b'strpos(self::CSRFC3548$chr));
  220.     }
  221. }

Documentation generated on Tue, 19 Nov 2013 14:54:26 +0100 by phpDocumentor 1.4.3