Skip to content
Advertisement

How can I store random_bytes in a database in PHP?

Inside of my migration I have a string column named encryption_key which looks like this:

JavaScript

I have a Controller that uses a trait to generate the encryption commands.

JavaScript

The trait looks like this so far

JavaScript

However, upon testing this I receive this error:

SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect string value: ‘xFFXx8Afx1F$…’ for column ‘encryption_key’ at row 1 (SQL: insert into chat_headers (encryption_key) values (ÿXŠfx1F$¨ì™ÒÂø¢Ú!£”…¸ÈÍØ7ÿDåx00Œ¿3ê))

If I use dd() to debug the response of $this->issueKey() I get something like this:

JavaScript

I have tried to use iconv() like so

JavaScript

Which gives me

iconv(): Detected an illegal character in input string

How can I store this key to use for future reference inside my DB?

Advertisement

Answer

You can base64_encode it for storage, and undo it with base64_decode.

Putting together a set of accessor and mutator functions will let you do this in an automatic fashion.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement