HOTP.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /*
  3. * Copyright (c) 2011 Le Lag
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. namespace OTPHP {
  22. /**
  23. * HOTP - One time password generator
  24. *
  25. * The HOTP class allow for the generation
  26. * and verification of one-time password using
  27. * the HOTP specified algorithm.
  28. *
  29. * This class is meant to be compatible with
  30. * Google Authenticator
  31. *
  32. * This class was originally ported from the rotp
  33. * ruby library available at https://github.com/mdp/rotp
  34. */
  35. class HOTP extends OTP {
  36. /**
  37. * Get the password for a specific counter value
  38. * @param integer $count the counter which is used to
  39. * seed the hmac hash function.
  40. * @return integer the One Time Password
  41. */
  42. public function at($count) {
  43. return $this->generateOTP($count);
  44. }
  45. /**
  46. * Verify if a password is valid for a specific counter value
  47. *
  48. * @param integer $otp the one-time password
  49. * @param integer $counter the counter value
  50. * @return bool true if the counter is valid, false otherwise
  51. */
  52. public function verify($otp, $counter) {
  53. return ($otp == $this->at($counter));
  54. }
  55. /**
  56. * Returns the uri for a specific secret for hotp method.
  57. * Can be encoded as a image for simple configuration in
  58. * Google Authenticator.
  59. *
  60. * @param string $name the name of the account / profile
  61. * @param integer $initial_count the initial counter
  62. * @return string the uri for the hmac secret
  63. */
  64. public function provisioning_uri($name, $initial_count) {
  65. return "otpauth://hotp/".urlencode($name)."?secret={$this->secret}&counter=$initial_count";
  66. }
  67. }
  68. }