Guest User

Untitled

a guest
Aug 11th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Workshop\Step07;
  4.  
  5.  
  6. /**
  7. * @entity
  8. */
  9. class Auth extends SimpleEntity
  10. {
  11. /**
  12. * @OneToOne(targetEntity="Workshop\Step07\Person", inversedBy="auth")
  13. * @JoinColumn(name="person_id", referencedColumnName="id")
  14. */
  15. private $person;
  16.  
  17. /** @Column(length=64) */
  18. private $username;
  19.  
  20. /** @Column(length=64) */
  21. private $hash;
  22.  
  23.  
  24. public function setUsername($username)
  25. {
  26. if (!$username = self::filterUsername($username)) {
  27. throw new \Exception(__('Username is a required entry.'));
  28. }
  29. $this->username = $username;
  30. return $this;
  31. }
  32.  
  33.  
  34. public function setHash($hash)
  35. {
  36. if (!$hash = self::filterHash($hash)) {
  37. throw new \Exception(__('Password is a required entry.'));
  38. }
  39. $this->hash = $hash;
  40. return $this;
  41. }
  42.  
  43.  
  44. public function isEqualPassword($password)
  45. {
  46. return $this->hash == self::hash($password, $this->username);
  47. }
  48.  
  49.  
  50. public static function hash($password, $username)
  51. {
  52. if (!$username = self::filterUsername($username)) {
  53. throw new \Exception(__('Username is a required entry.'));
  54. }
  55. if (!$password = self::filterPassword($password)) {
  56. throw new \Exception(__('Password is a required entry.'));
  57. }
  58. return self::filterHash(hash_hmac('sha256', $password, $username));
  59. }
  60. }
Add Comment
Please, Sign In to add comment