-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCryptCommands.php
More file actions
92 lines (83 loc) · 2.18 KB
/
CryptCommands.php
File metadata and controls
92 lines (83 loc) · 2.18 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
namespace Drush\Commands\drush_utility;
use Drupal\Component\Utility\Crypt;
use Drush\Commands\DrushCommands;
use Robo\ResultData;
/**
* Class CryptCommands
*
* @package Drush\Commands\drush_utility
*
* @see \Drupal\Component\Utility\Crypt
*/
class CryptCommands extends DrushCommands {
/**
* Calculates a base-64 encoded, URL-safe sha-256 hmac.
*
* @param $data
* Scalar value to be validated with the hmac.
* @param $key
* A secret key, this can be any scalar value.
*
* @return string
* A base-64 encoded sha-256 hmac, with + replaced with -, / with _ and
* any = padding characters removed.
*
* @bootstrap none
* @command crypt:hmac
*/
public function hmacBase64($data, $key): string {
return Crypt::hmacBase64($data, $key);
}
/**
* Calculates a base-64 encoded, URL-safe sha-256 hash.
*
* @param $data
* String to be hashed.
*
* @return string
* A base-64 encoded sha-256 hash, with + replaced with -, / with _ and
* any = padding characters removed.
*
* @bootstrap none
* @command crypt:hash
*/
public function hashBase64($data): string {
return Crypt::hashBase64($data);
}
/**
* Compares strings in constant time.
*
* @param string $known_string
* The expected string.
* @param string $user_string
* The user supplied string to check.
*
* @return \Robo\ResultData
*
* @bootstrap none
* @command crypt:hash-equals
*/
public function hashEquals($known_string, $user_string): ResultData {
if (Crypt::hashEquals($known_string, $user_string)) {
$this->io()->success(NULL);
return new ResultData(ResultData::EXITCODE_OK);
}
return new ResultData(ResultData::EXITCODE_ERROR);
}
/**
* Returns a URL-safe, base64 encoded string of highly randomized bytes.
*
* @param int $count
* The number of random bytes to fetch and base64 encode.
*
* @return string
* The base64 encoded result will have a length of up to 4 * $count.
*
* @bootstrap none
* @command crypt:random-bytes
*/
public function randomBytesBase64(int $count = 32): string {
return Crypt::randomBytesBase64($count);
}
}