/mod/oauth_api/vendors/oauth/library/signature_method/OAuthSignatureMethod_HMAC_SHA1.php

https://github.com/wangaiying/elgg4ysu · PHP · 115 lines · 49 code · 14 blank · 52 comment · 3 complexity · 3682b81a2619b085632e77315a001bd8 MD5 · raw file

  1. <?php
  2. /**
  3. * OAuth signature implementation using HMAC-SHA1
  4. *
  5. * @version $Id$
  6. * @author Marc Worrell <marcw@pobox.com>
  7. * @date Sep 8, 2008 12:21:19 PM
  8. *
  9. * The MIT License
  10. *
  11. * Copyright (c) 2007-2008 Mediamatic Lab
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in
  21. * all copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  29. * THE SOFTWARE.
  30. */
  31. require_once dirname(__FILE__).'/OAuthSignatureMethod.class.php';
  32. class OAuthSignatureMethod_HMAC_SHA1 extends OAuthSignatureMethod
  33. {
  34. public function name ()
  35. {
  36. return 'HMAC-SHA1';
  37. }
  38. /**
  39. * Calculate the signature using HMAC-SHA1
  40. * This function is copyright Andy Smith, 2007.
  41. *
  42. * @param OAuthRequest request
  43. * @param string base_string
  44. * @param string consumer_secret
  45. * @param string token_secret
  46. * @return string
  47. */
  48. function signature ( $request, $base_string, $consumer_secret, $token_secret )
  49. {
  50. $key = $request->urlencode($consumer_secret).'&'.$request->urlencode($token_secret);
  51. if (function_exists('hash_hmac'))
  52. {
  53. $signature = base64_encode(hash_hmac("sha1", $base_string, $key, true));
  54. }
  55. else
  56. {
  57. $blocksize = 64;
  58. $hashfunc = 'sha1';
  59. if (strlen($key) > $blocksize)
  60. {
  61. $key = pack('H*', $hashfunc($key));
  62. }
  63. $key = str_pad($key,$blocksize,chr(0x00));
  64. $ipad = str_repeat(chr(0x36),$blocksize);
  65. $opad = str_repeat(chr(0x5c),$blocksize);
  66. $hmac = pack(
  67. 'H*',$hashfunc(
  68. ($key^$opad).pack(
  69. 'H*',$hashfunc(
  70. ($key^$ipad).$base_string
  71. )
  72. )
  73. )
  74. );
  75. $signature = base64_encode($hmac);
  76. }
  77. return $request->urlencode($signature);
  78. }
  79. /**
  80. * Check if the request signature corresponds to the one calculated for the request.
  81. *
  82. * @param OAuthRequest request
  83. * @param string base_string data to be signed, usually the base string, can be a request body
  84. * @param string consumer_secret
  85. * @param string token_secret
  86. * @param string signature from the request, still urlencoded
  87. * @return string
  88. */
  89. public function verify ( $request, $base_string, $consumer_secret, $token_secret, $signature )
  90. {
  91. $a = $request->urldecode($signature);
  92. $b = $request->urldecode($this->signature($request, $base_string, $consumer_secret, $token_secret));
  93. // We have to compare the decoded values
  94. $valA = base64_decode($a);
  95. $valB = base64_decode($b);
  96. // Crude binary comparison
  97. return rawurlencode($a) == rawurlencode($b);
  98. }
  99. }
  100. /* vi:set ts=4 sts=4 sw=4 binary noeol: */
  101. ?>