0

I'm generating a salt using the openssl_random_pseudo_bytes function in php. However, I am unable to store the output into a PostgreSQL database.

I get an error:

ERROR: invalid byte sequence for encoding "UTF8"

Now I realize the encoding of the database in question is set to UTF8, and that the output of the function doesn't match, but what is the correct way of solving this?

This is a random salt, and I'm worried that converting it to UTF8 via whatever process will not make it cryptographically safe anymore?

5
  • 2
    It is binary data, so treat it as such. That means LOB in PDO or equivalent in the adapter of your choice. Commented Mar 6, 2013 at 7:57
  • 1
    Converting to UTF8 is operation on text data, it has nothing to do with binary data. You haven't shown your code but it seems you have error in that - you are trying to convert binary into string. Commented Mar 6, 2013 at 8:03
  • OK, yeah, I'm trying to store it in a column that is a data type of VARCHAR, so I guess I need to change the data type. What is the most appropriate? Commented Mar 6, 2013 at 8:06
  • 1
    see postgresql.org/docs/9.2/interactive/datatype-binary.html Commented Mar 6, 2013 at 8:09
  • OK cool, I was busy checking out bytea but wasn't 100% it was the correct data type. Thanks. Commented Mar 6, 2013 at 8:11

1 Answer 1

1

The openssl_random_pseudo_bytes function outputs binary data, and I was trying to store the output in a database field of data type character varying, mistakenly treating it like a string.

bytea is the correct data type for the field in the PostgreSQL database.

http://www.postgresql.org/docs/9.2/interactive/datatype-binary.html

Update: Also, it seems you need to use pg_escape_bytea on the output to be able to actually insert the binary data into the database.

Thanks to deceze, lechlukasz, and VolkerK in the comments for providing the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.