Source for file rfc2104.php

Documentation is available at rfc2104.php

  1. <?php
  2. /**
  3. * phpsnmp - a PHP SNMP library
  4. *
  5. * Copyright (C) 2004 David Eder <david@eder,us>
  6. *
  7. * Based on snmp - a Python SNMP library
  8. * Copyright (C) 2003 Unicity Pty Ltd <libsnmp@unicity.com.au>
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. * @author David Eder <david@eder.us>
  25. * @copyright 2004 David Eder
  26. * @package phpSNMP
  27. * @subpackage rfc2104
  28. * @version .7
  29. */
  30.  
  31. /**
  32. */
  33.  
  34. /**
  35. * generate HMAC
  36. *
  37. * @param string $data
  38. * @param string $key
  39. * @param string $hash_function
  40. * @param integer $block_size
  41. * @return string
  42. */
  43. function HMAC($data, $key, $hash_function='md5', $block_size=64)
  44. {
  45. if(!is_callable($hash_function))
  46. {
  47. trigger_error("$hash_function does not exist.", E_USER_WARNING);
  48. return '';
  49. }
  50.  
  51. if(strlen($key) > $block_size) $key = pack('H*', $hash_function($key));
  52.  
  53. $key = str_pad($key, $block_size, chr(0));
  54.  
  55. $ipad = $key ^ str_repeat(chr(0x36), $block_size);
  56. $opad = $key ^ str_repeat(chr(0x5c), $block_size);
  57.  
  58. $digest = pack('H*', $hash_function($ipad . $data));
  59.  
  60. return pack('H*', $hash_function($opad . $digest));
  61. }
  62.  
  63. ?>

Documentation generated on Mon, 14 Nov 2005 17:55:35 -0700 by phpDocumentor 1.3.0RC3