-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathToken.php
49 lines (44 loc) · 1.23 KB
/
Token.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
namespace App;
/* Token class to generate unique, random tokens */
/*
$token = new \App\Token();
*/
class Token {
/**
* @var array
*/
protected $token;
/**
* MAGIC METHOD: __construct
* @param void :
* @return void : Run this code when a new instance of Token is created
*/
public function __construct($token_value = null)//optional argument
{
if ($token_value) {
// Set the value at index "token" for the calling Token object equal to the parameter
$this->token = $token_value;
} else {
// Create a new token by generating random bytes and then converting binary to hex
$this->token = bin2hex(random_bytes(16));
}
}
/**
* METHOD: getHash
* @param void :
* @return string : Return the hashed value of the token
*/
public function getHash() {
return hash_hmac('sha256',$this->token,\App\Config::HASH_KEY);//sha256 = 64 chars
}
/**
* METHOD: getValue
* @param void :
* @return string : Return the current string saved at the $token index of the calling Token object
*/
public function getValue() {
return $this->token;
}
}
?>