a7omiton
a7omiton

Reputation: 1617

PHP mcrypt_create_iv returns question marks and incorrect length - CodeIgniter

I'm trying to use mcrypt_create_iv to generate random salts. When I test to see if the salt is generated by echo'ing it out, it checks out but it isn't the required length which I pass as a parameter to it (32), instead its less than that.

When I store it in my database table however, it shows up as something like this K??5P?M???4?o???"?0??

I'm sure it's something to do with the database, but I tried to change the collation of it to correspond with the config settings of CI, which is utf8_general_ci, but it doesn't solve the problem, instead it generates a much smaller salt.

Does anyone know of what may be wrong? Thanks for any feedback/help

Upvotes: 0

Views: 1318

Answers (1)

martinstoeckli
martinstoeckli

Reputation: 24141

The function mcrypt_create_iv() will return a binary string, containing \0 and other unreadable characters. Depending on how you want to use the salts, you first have to encode those byte strings, to an accepted alphabet. It is also possible to store binary strings in the database, but of course you will have a problem to display them.

Since salts are normally used for password storing, i would recommend to have a look at PHP's function password_hash(), it will generate a salt automatically and includes it in the resulting hash-value, so you don't need a separate database field for the salt.

Upvotes: 1

Related Questions