Skip to content

Commit 02e8504

Browse files
ircmaxellweltling
authored andcommitted
Refactor password_hash to use random_bytes internally to generate salts
1 parent 6a35e3b commit 02e8504

File tree

1 file changed

+5
-31
lines changed

1 file changed

+5
-31
lines changed

ext/standard/password.c

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "base64.h"
3131
#include "zend_interfaces.h"
3232
#include "info.h"
33+
#include "php_random.h"
3334

3435
#if PHP_WIN32
3536
#include "win32/winutil.h"
@@ -123,37 +124,10 @@ static int php_password_make_salt(size_t length, char *ret) /* {{{ */
123124

124125
buffer = (char *) safe_emalloc(raw_length, 1, 1);
125126

126-
#if PHP_WIN32
127-
{
128-
BYTE *iv_b = (BYTE *) buffer;
129-
if (php_win32_get_random_bytes(iv_b, raw_length) == SUCCESS) {
130-
buffer_valid = 1;
131-
}
132-
}
133-
#else
134-
{
135-
int fd, n;
136-
size_t read_bytes = 0;
137-
fd = open("/dev/urandom", O_RDONLY);
138-
if (fd >= 0) {
139-
while (read_bytes < raw_length) {
140-
n = read(fd, buffer + read_bytes, raw_length - read_bytes);
141-
if (n < 0) {
142-
break;
143-
}
144-
read_bytes += (size_t) n;
145-
}
146-
close(fd);
147-
}
148-
if (read_bytes >= raw_length) {
149-
buffer_valid = 1;
150-
}
151-
}
152-
#endif
153-
if (!buffer_valid) {
154-
for (i = 0; i < raw_length; i++) {
155-
buffer[i] ^= (char) (255.0 * php_rand() / RAND_MAX);
156-
}
127+
if (FAILURE == php_random_bytes_silent(buffer, raw_length)) {
128+
php_error_docref(NULL, E_WARNING, "Unable to generate salt");
129+
efree(buffer);
130+
return FAILURE;
157131
}
158132

159133
result = safe_emalloc(length, 1, 1);

0 commit comments

Comments
 (0)