-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hash.php
72 lines (52 loc) · 1.74 KB
/
Hash.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
use stdClass;
namespace Rubeus\SecretManagerGcp;
class Hash
{
private static function options($memoryCost, $timeCost, $threads)
{
$options = [
'memory_cost' => $memoryCost,
'time_cost' => $timeCost,
'threads' => $threads
];
return $options;
}
public static function create($string, $memoryCost = 1<<15, $timeCost = 4, $threads = 2)
{
$options = Hash::options($memoryCost, $timeCost, $threads);
$secretValue = password_hash($string, PASSWORD_ARGON2I, $options);
return $secretValue;
}
public static function verify($string, $secretKey)
{
$output = false;
$output = password_verify($string, $secretKey) ? true : false;
return $output;
}
public static function toString($secretObject)
{
$output = "$".$secretObject->algorithm;
$output .= "$".$secretObject->version;
$output .= "$".$secretObject->memory_cost;
$output .= ",".$secretObject->time_cost;
$output .= ",".$secretObject->parallelism;
$output .= "$".$secretObject->salt;
$output .= "$".$secretObject->secret;
return $output;
}
public static function toObject($secretValue)
{
$secretDetails = explode("$",$secretValue);
$costDetails = explode(",",$secretDetails[3]);
$output = new stdClass;
$output->algorithm = $secretDetails[1];
$output->version = $secretDetails[2];
$output->memory_cost = $costDetails[0];
$output->time_cost = $costDetails[1];
$output->parallelism = $costDetails[2];
$output->salt = $secretDetails[4];
$output->secret = $secretDetails[5];
return $output;
}
}