PluginProbe
ManageWP Worker / 4.0.1
ManageWP Worker v4.0.1
4.9.38 4.9.37 4.9.36 4.9.35 4.9.34 3.8.7 3.8.8 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.18 3.9.19 3.9.2 3.9.20 3.9.21 3.9.22 3.9.23 3.9.24 All 73 releases
worker / src / Dropbox / Security.php

Security.php in ManageWP Worker 4.0.1, at src/Dropbox/Security.php

72 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Helper functions for security-related things.
5 */
6 class Dropbox_Security
7 {
8 /**
9 * A string equality function that compares strings in a way that isn't suceptible to timing
10 * attacks. An attacker can figure out the length of the string, but not the string's value.
11 *
12 * Use this when comparing two strings where:
13 * - one string could be influenced by an attacker
14 * - the other string contains data an attacker shouldn't know
15 *
16 * @param string $a
17 * @param string $b
18 *
19 * @return bool
20 */
21 public static function stringEquals($a, $b)
22 {
23 // Be strict with arguments. PHP's liberal types could get us pwned.
24 if (func_num_args() !== 2) {
25 throw new InvalidArgumentException("Expecting 2 args, got ".func_num_args().".");
26 }
27 Dropbox_Checker::argString("a", $a);
28 Dropbox_Checker::argString("b", $b);
29
30 if (strlen($a) !== strlen($b)) {
31 return false;
32 }
33 $result = 0;
34 for ($i = 0; $i < strlen($a); $i++) {
35 $result |= ord($a[$i]) ^ ord($b[$i]);
36 }
37
38 return $result === 0;
39 }
40
41 /**
42 * Returns cryptographically strong secure random bytes (as a PHP string).
43 *
44 * @param int $numBytes
45 * The number of bytes of random data to return.
46 *
47 * @return string
48 */
49 public static function getRandomBytes($numBytes)
50 {
51 Dropbox_Checker::argIntPositive("numBytes", $numBytes);
52
53 // openssl_random_pseudo_bytes had some issues prior to PHP 5.3.4
54 if (function_exists('openssl_random_pseudo_bytes')
55 && version_compare(PHP_VERSION, '5.3.4') >= 0
56 ) {
57 $s = openssl_random_pseudo_bytes($numBytes, $isCryptoStrong);
58 if ($isCryptoStrong) {
59 return $s;
60 }
61 }
62
63 if (function_exists('mcrypt_create_iv')) {
64 return mcrypt_create_iv($numBytes);
65 }
66
67 // Hopefully the above two options cover all our users. But if not, there are
68 // other platform-specific options we could add.
69 throw new Exception("no suitable random number source available");
70 }
71 }
72