includes/hmac.php
changeset 801 eb8b23f11744
child 841 83bb60402f51
equal deleted inserted replaced
800:9cdfe82c56cd 801:eb8b23f11744
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
       
     5  * Version 1.1.6 (Caoineag beta 1)
       
     6  * Copyright (C) 2006-2008 Dan Fuhry
       
     7  * hmac.php - HMAC cryptographic functions
       
     8  *
       
     9  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
       
    10  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
       
    11  *
       
    12  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
       
    13  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
       
    14  */
       
    15 
       
    16 function hmac_gen_padding($val, $len = 32)
       
    17 {
       
    18   $ret = array();
       
    19   for ( $i = 0; $i < $len; $i++ )
       
    20   {
       
    21     $ret[] = $val;
       
    22   }
       
    23   return $ret;
       
    24 }
       
    25 
       
    26 function hmac_core($message, $key, $hashfunc)
       
    27 {
       
    28   static $block_sizes = array();
       
    29   if ( !isset($block_sizes[$hashfunc]) )
       
    30   {
       
    31     $block_sizes[$hashfunc] = strlen($hashfunc(''))/2;
       
    32   }
       
    33   $blocksize = $block_sizes[$hashfunc];
       
    34   $ipad = hmac_gen_padding(0x5c, $blocksize);
       
    35   $opad = hmac_gen_padding(0x36, $blocksize);
       
    36   if ( strlen($key) != ( $blocksize * 2 ) )
       
    37     $key = $hashfunc($key);
       
    38   $key = hmac_hexbytearray($key);
       
    39   for ( $i = 0; $i < count($key); $i++ )
       
    40   {
       
    41     $ipad[$i] = $ipad[$i] ^ $key[$i];
       
    42     $opad[$i] = $opad[$i] ^ $key[$i];
       
    43   }
       
    44   return $hashfunc(hmac_bytearraytostring($opad) . $hashfunc(hmac_bytearraytostring($ipad) . $message));
       
    45 }
       
    46 
       
    47 function hmac_hexbytearray($val)
       
    48 {
       
    49   $val = hexdecode($val);
       
    50   return hmac_bytearray($val);
       
    51 }
       
    52 
       
    53 function hmac_bytearray($val)
       
    54 {
       
    55   $val = str_split($val, 1);
       
    56   foreach ( $val as &$char )
       
    57   {
       
    58     $char = ord($char);
       
    59   }
       
    60   return $val;
       
    61 }
       
    62 
       
    63 function hmac_bytearraytostring($val)
       
    64 {
       
    65   foreach ( $val as &$char )
       
    66   {
       
    67     $char = chr($char);
       
    68   }
       
    69   return implode('', $val);
       
    70 }
       
    71 
       
    72 function hmac_md5($message, $key)
       
    73 {
       
    74   return hmac_core($message, $key, 'md5');
       
    75 }
       
    76 
       
    77 function hmac_sha1($message, $key)
       
    78 {
       
    79   return hmac_core($message, $key, 'sha1');
       
    80 }
       
    81 
       
    82 function hmac_sha256($message, $key)
       
    83 {
       
    84   require_once(ENANO_ROOT . '/includes/math.php');
       
    85   require_once(ENANO_ROOT . '/includes/diffiehellman.php');
       
    86   return hmac_core($message, $key, 'sha256');
       
    87 }
       
    88 
       
    89 ?>