comparison vendor/paragonie/random_compat/lib/random_bytes_dev_urandom.php @ 16:c2387f117808

Routine composer update
author Chris Cannam
date Tue, 10 Jul 2018 15:07:59 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
comparison
equal deleted inserted replaced
15:e200cb7efeb3 16:c2387f117808
3 * Random_* Compatibility Library 3 * Random_* Compatibility Library
4 * for using the new PHP 7 random_* API in PHP 5 projects 4 * for using the new PHP 7 random_* API in PHP 5 projects
5 * 5 *
6 * The MIT License (MIT) 6 * The MIT License (MIT)
7 * 7 *
8 * Copyright (c) 2015 - 2017 Paragon Initiative Enterprises 8 * Copyright (c) 2015 - 2018 Paragon Initiative Enterprises
9 * 9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy 10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal 11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights 12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
44 * 44 *
45 * @return string 45 * @return string
46 */ 46 */
47 function random_bytes($bytes) 47 function random_bytes($bytes)
48 { 48 {
49 /** @var resource $fp */
49 static $fp = null; 50 static $fp = null;
51
50 /** 52 /**
51 * This block should only be run once 53 * This block should only be run once
52 */ 54 */
53 if (empty($fp)) { 55 if (empty($fp)) {
54 /** 56 /**
55 * We use /dev/urandom if it is a char device. 57 * We use /dev/urandom if it is a char device.
56 * We never fall back to /dev/random 58 * We never fall back to /dev/random
57 */ 59 */
60 /** @var resource|bool $fp */
58 $fp = fopen('/dev/urandom', 'rb'); 61 $fp = fopen('/dev/urandom', 'rb');
59 if (!empty($fp)) { 62 if (is_resource($fp)) {
63 /** @var array<string, int> $st */
60 $st = fstat($fp); 64 $st = fstat($fp);
61 if (($st['mode'] & 0170000) !== 020000) { 65 if (($st['mode'] & 0170000) !== 020000) {
62 fclose($fp); 66 fclose($fp);
63 $fp = false; 67 $fp = false;
64 } 68 }
65 } 69 }
66 70
67 if (!empty($fp)) { 71 if (is_resource($fp)) {
68 /** 72 /**
69 * stream_set_read_buffer() does not exist in HHVM 73 * stream_set_read_buffer() does not exist in HHVM
70 * 74 *
71 * If we don't set the stream's read buffer to 0, PHP will 75 * If we don't set the stream's read buffer to 0, PHP will
72 * internally buffer 8192 bytes, which can waste entropy 76 * internally buffer 8192 bytes, which can waste entropy
81 } 85 }
82 } 86 }
83 } 87 }
84 88
85 try { 89 try {
90 /** @var int $bytes */
86 $bytes = RandomCompat_intval($bytes); 91 $bytes = RandomCompat_intval($bytes);
87 } catch (TypeError $ex) { 92 } catch (TypeError $ex) {
88 throw new TypeError( 93 throw new TypeError(
89 'random_bytes(): $bytes must be an integer' 94 'random_bytes(): $bytes must be an integer'
90 ); 95 );
101 * 106 *
102 * It does not belong in an else {} block, because the above 107 * It does not belong in an else {} block, because the above
103 * if (empty($fp)) line is logic that should only be run once per 108 * if (empty($fp)) line is logic that should only be run once per
104 * page load. 109 * page load.
105 */ 110 */
106 if (!empty($fp)) { 111 if (is_resource($fp)) {
107 /** 112 /**
108 * @var int 113 * @var int
109 */ 114 */
110 $remaining = $bytes; 115 $remaining = $bytes;
111 116