Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP random_bytes returns unreadable characters

Tags:

php

mysql

I am using:

 random_bytes(30);

to generate a random string for my program. This string is then stored in a mysql database. However, when I run the function, I get an output such as

T�Ը�@(���m

which includes unreadable characters. When I then try to store it in my database, the � characters are not stored and so this means that the string is not properly preserved.

How can I make it so that it returns a string with only readable characters that will not be lost in my mysql database?

Thanks!

like image 659
Onglo Avatar asked Oct 29 '22 05:10

Onglo


1 Answers

random_bytes generates an arbitrary length string of cryptographic random bytes that are suitable for cryptographic use, such as when generating salts, keys or initialization vectors. use like this

$bytes = random_bytes(5);
echo bin2hex($bytes);
like image 154
RAUSHAN KUMAR Avatar answered Nov 15 '22 05:11

RAUSHAN KUMAR