|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* The MIT License (MIT) |
|
4
|
|
|
* Copyright (c) 2017 Angel Cruz <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
7
|
|
|
* of this software and associated documentation files (the “Software”), to deal |
|
8
|
|
|
* in the Software without restriction, including without limitation the rights |
|
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
11
|
|
|
* furnished to do so, subject to the following conditions: |
|
12
|
|
|
* |
|
13
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
14
|
|
|
* all copies or substantial portions of the Software. |
|
15
|
|
|
* |
|
16
|
|
|
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
22
|
|
|
* THE SOFTWARE. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Angel Cruz <[email protected]> |
|
25
|
|
|
* @license MIT License |
|
26
|
|
|
* @copyright 2017 Angel Cruz |
|
27
|
|
|
*/ |
|
28
|
|
|
|
|
29
|
|
|
namespace Abr4xas\Utils; |
|
30
|
|
|
|
|
31
|
|
|
class Hash |
|
32
|
|
|
{ |
|
33
|
|
|
|
|
34
|
|
|
const ALGO = PASSWORD_BCRYPT; |
|
35
|
|
|
const COST = 10; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct() |
|
38
|
|
|
{ |
|
39
|
|
|
$this->algo = self::ALGO; |
|
|
|
|
|
|
40
|
|
|
$this->cost = self::COST; |
|
|
|
|
|
|
41
|
|
|
} //End constructor |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Hash a plain text password. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $password The plain text password to hash. |
|
47
|
|
|
* @return string The hashed $password parameter. |
|
48
|
|
|
*/ |
|
49
|
|
|
public function password($password) |
|
50
|
|
|
{ |
|
51
|
|
|
return password_hash($password, $this->algo, ['cost' => $this->cost]); |
|
52
|
|
|
} //End password |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Check a password against it's hash. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $password Plain text password. |
|
58
|
|
|
* @param string $hash Hashed password. |
|
59
|
|
|
* @return bool True if they match, false otherwise. |
|
60
|
|
|
*/ |
|
61
|
|
|
public function passwordCheck($password, $hash) |
|
62
|
|
|
{ |
|
63
|
|
|
return password_verify($password, $hash); |
|
64
|
|
|
} //End password |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Use sha256 to hash an input string. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $input Plain text string to be hashed. |
|
70
|
|
|
* @return string The hashed version of the $input string. |
|
71
|
|
|
*/ |
|
72
|
|
|
public function hash($input) |
|
73
|
|
|
{ |
|
74
|
|
|
return hash('sha256', $input); |
|
75
|
|
|
} //End hash |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Check a hash against another. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $know The hash we know is correct. |
|
81
|
|
|
* @param string $unknown The hash we want to compare with it. |
|
82
|
|
|
* @return bool True if the hashed strings match, false otherwise. |
|
83
|
|
|
*/ |
|
84
|
|
|
public function hashCheck($know, $unknown) |
|
85
|
|
|
{ |
|
86
|
|
|
return hash_equals($know, $unknown); |
|
87
|
|
|
} //End hashCheck |
|
88
|
|
|
|
|
89
|
|
|
} //End class Hash |