Hash the password and save it, and salt in mysql pdo utf8 encoding

I have this question, excuse me if this does not fit, tell me in the comments and I will drop it. The point is: I generate salt using this code:

$salt = mcrypt_create_iv(32);

And the password this way:

$password =  hash('sha256', $_POST['password'] . $salt);

and save $ salt and $ password in the MYSQL database until there is no problem if I connect this path:

$pdo = new PDO(
    'mysql:host=hostname;dbname=defaultDbName',
    'username',
    'password'
);

But if I connect this other way:

$pdo = new PDO(
    'mysql:host=hostname;dbname=defaultDbName;charset=utf8',
    'username',
    'password'
);

salt is not stored in the database or is stored with the wrong value and length; if I do not do this: $ salt = utf8_encode (mcrypt_create_iv (32);); But I think this is wrong, I mean, why should I encode this, what is the problem with mcrypt_create_iv (32) and utf-8?

+4
source share
2

:

mcrypt_create_iv , . , UTF-8. utf8_encode , utf8_decode .

utf8, () ANSI-, - .

: :

, , (BINARY, VARBINARY BLOB) , - .

BINARY, , , , .

. http://dev.mysql.com/doc/refman/5.0/en/binary-varbinary.html

: , :

bin2hex . , . hex2bin.

+4

, mcrypt_create_iv , utf8. - :

function generate_random($length=16, $simple=false){
        $result='';
        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()+=-_?~';
        if($simple)
            $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
        for ($i = 0; $i < $length; $i++)
            $result .= $chars[mt_rand(0, strlen($chars)-1)];
        return $result;
    }

16 78 1.877 * 10 ^ 30 . , sha256.

0

Source: https://habr.com/ru/post/1539344/


All Articles