PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/pear/OAuth/Signature/hmac_sha1.php

https://bitbucket.org/chamilo/chamilo/
PHP | 130 lines | 38 code | 12 blank | 80 comment | 2 complexity | 456536dc42004f027a4c8c4f89d5bde5 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. /**
  3. * OAuth Request Signing
  4. *
  5. * Adapted from Andy Smith's OAuth library for PHP
  6. *
  7. * @link http://oauth.net/core/1.0
  8. * @link http://oauth.googlecode.com/svn/code/php/
  9. * @link http://term.ie/oauth/example/
  10. *
  11. * @package OAuth
  12. * @subpackage Signature
  13. *
  14. * @author jhart
  15. * @copyright Copyright (c) 2008, Photobucket, Inc.
  16. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  17. */
  18. /**
  19. * interface
  20. */
  21. require_once('OAuth/Signature/Interface.php');
  22. /**
  23. * OAuth HMAC-SHA1 implementation
  24. *
  25. * @package OAuth
  26. * @subpackage Signature
  27. */
  28. class OAuth_Signature_hmac_sha1 implements OAuth_Signature_Interface {
  29. /**
  30. * Representation string
  31. *
  32. */
  33. const OAUTH_SIGNATURE_METHOD = 'HMAC-SHA1';
  34. /**
  35. * Sign a request
  36. *
  37. * @param OAuth_Request $request request to sign
  38. * @param string $consumer_secret consumer secret key
  39. * @param string $token_secret token secret key
  40. * @return string calculated hash for request, secrets
  41. */
  42. public function signRequest(OAuth_Request $request, $consumer_secret, $token_secret = '') {
  43. $basestr = self::generateBaseString(
  44. $request->getHttpMethod(),
  45. $request->getHttpUrl(),
  46. OAuth_Utils::normalizeKeyValueParameters(OAuth_Utils::getFilteredBaseStringParams($request->getParameters()))
  47. );
  48. //for debug purposes
  49. $request->base_string = $basestr;
  50. $keystr = self::generateKeyString($consumer_secret, $token_secret);
  51. //for debug purposes
  52. $request->key_string = $keystr;
  53. return self::calculateHash($basestr, $keystr);
  54. }
  55. /**
  56. * Get the OAuth official string representation for this method
  57. *
  58. * @return string oauth method name
  59. */
  60. public function getMethodName() {
  61. return self::OAUTH_SIGNATURE_METHOD;
  62. }
  63. /**
  64. * Creates the basestring needed for signing per oAuth Section 9.1.2
  65. * All strings are latin1
  66. *
  67. * @todo could be in a base class for hmac
  68. * @uses urlencodeRFC3986_UTF8()
  69. * @param string $http_method one of the http methods GET, POST, etc.
  70. * @param string $uri the uri; the url without querystring
  71. * @param string $params normalized parameters as returned from OAuthUtil::normalizeParameters
  72. * @return string concatenation of the encoded parts of the basestring
  73. */
  74. protected static function generateBaseString($http_method, $uri, $params) {
  75. return OAuth_Utils::urlencodeRFC3986($http_method)
  76. . '&' . OAuth_Utils::urlencodeRFC3986($uri)
  77. . '&' . OAuth_Utils::urlencodeRFC3986_UTF8($params);
  78. }
  79. /**
  80. * Generate a key string
  81. *
  82. * @todo could be in a base class for hmac
  83. * @param string $consumersecret consumer secret key
  84. * @param string $tokensecret token secret key
  85. * @return string single key string
  86. */
  87. protected static function generateKeyString($consumersecret, $tokensecret = '') {
  88. return OAuth_Utils::urlencodeRFC3986_UTF8($consumersecret)
  89. . '&' . OAuth_Utils::urlencodeRFC3986_UTF8($tokensecret);
  90. }
  91. /**
  92. * Calculates the HMAC-SHA1 secret
  93. *
  94. * @uses urlencodeRFC3986_UTF8()
  95. * @param string $basestring gotten from generateBaseString
  96. * @param string $consumersecret
  97. * @param string $tokensecret leave empty if no token present
  98. * @return string base64 encoded signature
  99. */
  100. protected static function calculateHash($basestring, $key) {
  101. return base64_encode(self::hash_hmac_sha1($basestring, $key, true));
  102. }
  103. /**
  104. * run hash_hmac with sha1 (package independant)
  105. *
  106. * @uses php_hash_hmac
  107. * @param string $string string to hash
  108. * @param string $key key to hash against
  109. * @return string result of hash
  110. */
  111. protected static function hash_hmac_sha1($string, $key, $raw = true) {
  112. if (function_exists('hash_hmac')) {
  113. return hash_hmac('sha1', $string, $key, $raw);
  114. } else {
  115. return OAuth_Utils::php_hash_hmac('sha1', $string, $key, $raw);
  116. }
  117. }
  118. }